Hi,
> My application is using a Dll loaded with the function
> "LoadLibrary(<NameOfTheDll>)"
[quoted text clipped - 7 lines]
> (...)// 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());
>
[quoted text clipped - 11 lines]
> 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[]
--
SvenC
JsCharly - 24 May 2007 10:00 GMT
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
> Hi,
> > My application is using a Dll loaded with the function
[quoted text clipped - 60 lines]
> --
> SvenC
SvenC - 24 May 2007 10:47 GMT
Hi,
> I would like to better understand one thing of your reply.
>
[quoted text clipped - 6 lines]
> 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.
--
SvenC