Here is my problem: I’m making a C++ DLL which relies extensively on instance object exports.
So I return my actual instances as a pointers to an interface through some exported factory method.
The interfaces I use are purely virtual, to avoid linking problems. So I need a pure virtual destructor too, and I implemented one (with empty body, as I googled it).
All compiles perfectly well, except… I can’t see if the actual destructors are called or not – because when I added some
std::cout << "hello destructor";
I never get to see it.
I have some explicit “delete obj” (EDIT: and it is called from the “FreeObject” method inside the DLL), that’s not the problem.
Am I missing something? Is there another way to delete my object through an interface?
EDIT: Again, I don’t have memory management inconsistency, it’s all inside the DLL. But the right destructor just isn’t called.
You shouldn’t mix and match calls to
newanddeleteacross DLL boundaries.Instead, I’d recommend the tried-and-true method that COM uses:
AddRefandRelease. When the reference count hits zero,Releasecallsdelete this;from inside the DLL, ensuring thatnewanddeleteare matched.