I have created a regular dll where I have one method GetRecord to allocate memory for RecordInfo structure.
struct RecordInfo{
// Some Data member
}
BOOL GetRecord(RecordInfo *& pRecordInfo)
{
// Allocate RecordInfo, fill data and return as out paramter.
}
Now I access this dlls from MFC application and after finish my work. I release RecordInfo in client using delete.
delete pRecordInfo;
In release mode there is no problem, However in debug mode application shows assertion failure. Can some one explain reason behind this behaviour.
If you want to allocate memory in a DLL and free it in client code, you should make sure that both the DLL and the client are built with the same version of the compiler and same CRT “flavor” (e.g. both are debug builds, or both are release builds).
This is of course highly constraining.
As an alternative, to decouple things better, you can export from the DLL functions to both allocate and deallocate your data structures (in your particular case you could add and export a
DeleteRecordfunction from your DLL, and calldeletefrom inside this function body).I suggest you reading this interesting blog post on the Old New Thing blog, and note especially the beginning sentence (emphasis mine):