An important rule is that the one who allocates the memory is responsible for freeing that memory using the same allocator . There is no restriction on how memory should be allocated (it can be assigned as the main executable file, your DLL or some other DLL), but after allocating it, the same module should free it.
, , - DLL , . , . :
class MyClass { ... };
MyClass * DLLEXPORT NewMyClass()
{
return new MyClass;
}
void DLLEXPORT FreeMyClass(MyClass *obj)
{
delete obj;
}
, , (, NewMyClass() ) (, ):
HMODULE mydll = LoadLibrary("mydll.dll");
MyClass (*NewMyClass)() = (MyClass (*)())GetProcAddress(mydll, "NewMyClass");
MyClass *obj = NewMyClass();
...
delete obj;
, ( ) , C/++. , , , malloc/free operator new/operator delete .
. .