CrtIsValidHeapPointer
1 November, 2006 2 Comments
/* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */
I got this interesting debug assertion the other day while removing an object from a queue in a window DLL. It all sounds fair enough I must be passing a bad pointer. This wasn’t the case the pointer was fine. Next option was that i wasn’t creating this object on the local heap. I was in fact pushing items onto this queue from another DLL so this could be the case but the function that pushes objects onto the queue creates them in the correct DLL using new and copies all the data passed in into this structure.
So this shouldn’t be happening!
The culprit turned out to be a CString. Even though i thought i was copying it into “new” memory the CString copy constructor and assignment operator just reference count strings until you change them. So even though i was passing a CString by value and copying it into an altogether new CString the buffer is still the same.
This is obvious in retrospect. Almost all string libraries perform shallow copies. The lesson never trust someones copy constructor to perform a “copy”
- Jared