CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: atlsimpstr.h access violation error

    Quote Originally Posted by saraswathisrinath View Post
    ok.

    I have a doubt, today i received below errors from atlsimpstr.h,

    1. nLength <=GetData() -> AllocLength

    2. nrefs != 0

    3. CStringData* pNewData = pOldData->Clone->Allocate(nLength, sizeof(XCHAR));
    if(pNewData == NULL ) ThrowMemoryException


    I refered https://social.msdn.microsoft.com/fo...dialog-pointer.
    From this link, i have a doubt from string handling.

    and confirmed using call stack.


    Is the problem is nonproper handling of String?
    A: 99.999% yes, is a problem of improperly handling of CString
    You can simply reproduce this behavior writing the following simple code:
    Code:
        CString* pString = new CString(_T("Geo luvs strings"));
        delete pString;
        // ...
        delete pString;
    However, why the assert complains about nRefs?
    An instance of a CString object has before its string buffer a CStringData structure.
    Beside others, CStringData structure has a member named nRefs which is almost like a reference counter of a shared smart pointer. It avoids to have more than one CString object with different buffers having the same string contents. When assign one CString object to another new one, the new one gets the same string buffer and just CStringData::nRefs is incremented. Practically we can have more CString objects but in fact, only one string buffer in memory.
    When delete one of "copies", CStringData::nRefs is decremented, and the string buffer is effectively deleted from memory only if CStringData::nRefs becomes 0 (zero).
    Last edited by ovidiucucu; March 27th, 2015 at 03:09 PM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured