Click to See Complete Forum and Search --> : Assertion in _CrtIsValidHeapPointer().


subhash_patil
July 20th, 2002, 08:09 AM
hi gurus,
I have a strange problem. I have a COM dll. This COM dll has settings as "Multithreaded" for release and "debug Multithreaded" for debug version. I have a test application which hosts this dll as an INPROC server. In the dll, i create a pointer to a class using new. I delete it immediately in the next line. Here, on delete, I get an assertion in _CrtIsValidHeapPointer()! which says that the memory address does not belong to local heap. If i am deleting immediately after new, why should this problem come?
e.g. The dll function looks like this.

STDMETHODIMP CData::Write(IStream *pStream)
{
// class defined in another lib statically linked to this dll.
CMyClass *pMyClass = new CMyClass;
delete pMyClass; // Assertion at this.
}

Interestingly, if i create a pointer to the same class using new in Test application and delete it , it works fine. So there seems some problem within the dll. Can anyone throw some light on this??

thanx in advance!

Anthony Mai
July 20th, 2002, 10:20 AM
This assertion failure tells you that you are attempting to deallocate memory that was allocated in a different heap space, so although the pointer is the same, they are not pointed to the same memory!!!

Further, this mean the allocation and deallocation of a piece of memory happens in different executable modules. Look at what you do in the constructor and destructor of your class, and where you do it.

Also, if different modules uses different settings for CRT (debug, debug multi-threaded, debug multi-threaded DLL), the described problem may occur. It is best to consistently use debug multi-threaded DLL for all modules.

Anthony Mai
July 20th, 2002, 10:26 AM
Here is what the comment in the CRT source code says about
this assertion:

/***
*int _CrtIsValidHeapPointer() - verify pointer is from 'local' heap
*
*Purpose:
* Verify pointer is not only a valid pointer but also that it is from
* the 'local' heap. Pointers from another copy of the C runtime (even in the
* same process) will be caught.
*
*Entry:
* const void * pUserData - pointer of interest
*
*Return:
* TRUE - if valid and from local heap
* FALSE otherwise
*
*******************************************************************************/