_CrtIsValidHeapPointer

G

Guest

Hello,

My application is using a Dll loaded with the function
"LoadLibrary(<NameOfTheDll>)"

In the dll there is the following fonction :

char * GetMessageError(int Error)
{

char * pMessageError;

CString ErrorMessage;

(...)// filling of Error message

pMessageError= new char[ErrorMessage.GetLength()+1];
strcpy_s(pMessageError,ErrorMessage.GetLength()+1,ErrorMessage.GetBuffer());

return pMessageError;
}

In my application there is the function :


void DisplayMessageError(int DllError)
{
char * pMessageError= GetMessageError(DllError);

if(pMessageError){

AfxMessageBox(pMessageError);
delete pMessageError;//!!!!!!!!!!!---------->Problem at this line
}
}


In debug mode when executing the line 'delete pMessageError'
I have the following assertion

/*
* 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.
*/
_ASSERTE(_CrtIsValidHeapPointer(pUserData));

The Dll seems to have its own heap.

How can I solve this problem ?

JsCHarly
 
S

SvenC

Hi,
My application is using a Dll loaded with the function
"LoadLibrary(<NameOfTheDll>)"

In the dll there is the following fonction :

char * GetMessageError(int Error)
{
char * pMessageError;
CString ErrorMessage;
(...)// filling of Error message
pMessageError= new char[ErrorMessage.GetLength()+1];

This is allocated in the heap of the current c++ runtime
strcpy_s(pMessageError,ErrorMessage.GetLength()+1,ErrorMessage.GetBuffer());

return pMessageError;
}

In my application there is the function :

void DisplayMessageError(int DllError)
{
char * pMessageError= GetMessageError(DllError);

if(pMessageError){

AfxMessageBox(pMessageError);
delete pMessageError;//!!!!!!!!!!!---------->Problem at this line

Two errors:
1) new [] must be complemented with delete []. So it should read "delete []
pMessageError;"

2) it is freed in this modules c++ runtime heap which might be different,
except when both are compiled against the same dll runtime version.
Thats what the error is telling you: wrong heap.

There are two common ways to overcome that problem:
a) let the caller allocate the memory and pass a pointer to that memory plus
the size of the buffer to the callee

void GetMessageError(int Error, char* buf, size_t bufSize)

char* p = new char[100];
GetMessageError(42, p, 100);
// use p
delete [] p;

b) Export a delete function in your dll which returns heap memory to its
callers

void FreeMemory(char *p); // implemented in the called dll

char* p = GetMessageError(42);
// use p
FreeMemory(p);

You might need different free functions for different types of allocated
memory. E.g. to differentiate between new/delete and new[]/delete[]
 
G

Guest

Thank you for your answer which solved my problem.

I would like to better understand one thing of your reply.

You wrote :
2) it is freed in this modules c++ runtime heap which might be different,
except when both are compiled against the same dll runtime version.

What do you mean by " except when both are compiled against the same dll
runtime version"

JsCHarly


SvenC said:
Hi,
My application is using a Dll loaded with the function
"LoadLibrary(<NameOfTheDll>)"

In the dll there is the following fonction :

char * GetMessageError(int Error)
{
char * pMessageError;
CString ErrorMessage;
(...)// filling of Error message
pMessageError= new char[ErrorMessage.GetLength()+1];

This is allocated in the heap of the current c++ runtime
strcpy_s(pMessageError,ErrorMessage.GetLength()+1,ErrorMessage.GetBuffer());

return pMessageError;
}

In my application there is the function :

void DisplayMessageError(int DllError)
{
char * pMessageError= GetMessageError(DllError);

if(pMessageError){

AfxMessageBox(pMessageError);
delete pMessageError;//!!!!!!!!!!!---------->Problem at this line

Two errors:
1) new [] must be complemented with delete []. So it should read "delete []
pMessageError;"

2) it is freed in this modules c++ runtime heap which might be different,
except when both are compiled against the same dll runtime version.
Thats what the error is telling you: wrong heap.

There are two common ways to overcome that problem:
a) let the caller allocate the memory and pass a pointer to that memory plus
the size of the buffer to the callee

void GetMessageError(int Error, char* buf, size_t bufSize)

char* p = new char[100];
GetMessageError(42, p, 100);
// use p
delete [] p;

b) Export a delete function in your dll which returns heap memory to its
callers

void FreeMemory(char *p); // implemented in the called dll

char* p = GetMessageError(42);
// use p
FreeMemory(p);

You might need different free functions for different types of allocated
memory. E.g. to differentiate between new/delete and new[]/delete[]
 
S

SvenC

Hi,
I would like to better understand one thing of your reply.

You wrote :


What do you mean by " except when both are compiled against the same
dll runtime version"

In the project settings you can choose to statically link to the crt which
will always give that module its own heap.
You can also choose to use the dll crt which exists as release and debug
version and is different for all VC++ compilers (6, 7.0, 7.1, 8.0, ...) So
any mixture of release/debug and/or compiler versions will also introduce
separate heaps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top