Strange problem with _CrtIsValidHeapPointer
-
When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.
class CData
{
public:
~CData() { }
DWORD nValue;
};This works of cause.
CData* pData = NULL;
pData = new CData;
_CrtIsValidHeapPointer( pData );
delete pData;This Does NOT Work
pData = new CData;
pData = new CLineInfo[ 2 ];
_CrtIsValidHeapPointer( pData ); // <<-- fails
delete[] pData;BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.
-
When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.
class CData
{
public:
~CData() { }
DWORD nValue;
};This works of cause.
CData* pData = NULL;
pData = new CData;
_CrtIsValidHeapPointer( pData );
delete pData;This Does NOT Work
pData = new CData;
pData = new CLineInfo[ 2 ];
_CrtIsValidHeapPointer( pData ); // <<-- fails
delete[] pData;BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.
-
When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.
class CData
{
public:
~CData() { }
DWORD nValue;
};This works of cause.
CData* pData = NULL;
pData = new CData;
_CrtIsValidHeapPointer( pData );
delete pData;This Does NOT Work
pData = new CData;
pData = new CLineInfo[ 2 ];
_CrtIsValidHeapPointer( pData ); // <<-- fails
delete[] pData;BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.
This error occurs when you alllocate memory in one heap and delete it in another heap. Check whether both Debug and Release mode use run time library settings are the same or not . Appu.. "If you judge people, you have no time to love them."
-
How does it work if instead of
pData = new CData; pData = new CLineInfo[ 2 ];
you have
pData = new CData[ 2 ];?
-
When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.
class CData
{
public:
~CData() { }
DWORD nValue;
};This works of cause.
CData* pData = NULL;
pData = new CData;
_CrtIsValidHeapPointer( pData );
delete pData;This Does NOT Work
pData = new CData;
pData = new CLineInfo[ 2 ];
_CrtIsValidHeapPointer( pData ); // <<-- fails
delete[] pData;BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.
Mathias S. wrote:
BUT if I remove the Destructor from the CData class. It WORKS ! Strange...
Try adding Constructer of CData class.. what is CLineInfo?? Is it derived from CData class. Knock out 't' from can't, You can if you think you can :cool:
-
This error occurs when you alllocate memory in one heap and delete it in another heap. Check whether both Debug and Release mode use run time library settings are the same or not . Appu.. "If you judge people, you have no time to love them."
I suspected that too. But if I do new and delete right after each other as in the example. they are run from the same place. I tryed changing runtime library too. I Have tested with both /MTd and /MDd but with no success. and If that was the error. Why does it works if I remove the destructor. It is that part that is messing with my head.. -Mathias S.
-
Mathias S. wrote:
BUT if I remove the Destructor from the CData class. It WORKS ! Strange...
Try adding Constructer of CData class.. what is CLineInfo?? Is it derived from CData class. Knock out 't' from can't, You can if you think you can :cool:
CLineInfo is what the class is called in my real code. That was a cut'n'paste error :) And it have constructor and a lot of variables im my real code, and I created this example and striped it down until _CrtIsValidHeapPointer did not complain anymore. and that happend when I removed the destuctor. I can have a Constructer there without any problems, as long as there is no desructor. -Mathias S.
-
When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.
class CData
{
public:
~CData() { }
DWORD nValue;
};This works of cause.
CData* pData = NULL;
pData = new CData;
_CrtIsValidHeapPointer( pData );
delete pData;This Does NOT Work
pData = new CData;
pData = new CLineInfo[ 2 ];
_CrtIsValidHeapPointer( pData ); // <<-- fails
delete[] pData;BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.
Please correct your post to fix the typos you mention in later posts. Does the
CLineInfo
overload itsoperator new
for custom heap/caching/allocation purposes? If so, that will likely raise the error because the heap management will be different. If you are allocating in one file and deleting in another, make sure that all files involved have the#define new DEBUG_NEW
(VC++ 6.0) at the top of the file, or you could end up using different heap routines. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
Please correct your post to fix the typos you mention in later posts. Does the
CLineInfo
overload itsoperator new
for custom heap/caching/allocation purposes? If so, that will likely raise the error because the heap management will be different. If you are allocating in one file and deleting in another, make sure that all files involved have the#define new DEBUG_NEW
(VC++ 6.0) at the top of the file, or you could end up using different heap routines. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)No! no overloading of new/delete. But I must have had som other error that is fixed now, that overwrote the memory or something. because delete[] work now. But _CrtIsValidPointer still does not work when checking a pointer of an array of objects with a destructors. Because of a bug in CrtIsValidHeapPointer. but as a workaround I can use _CrtIsValidHeapPointer( ((LPCBYTE)p)-4 ); - Mathias