I need help... destructors not firing?

Started by
3 comments, last by hplus0603 19 years, 6 months ago
My engine is in a DLL file. In the client application I load the DLL with LoadLibrary and I get an interface the exact way shown here: http://www.gamedev.net/reference/articles/article928.asp I then store a pointer to the interface in a member variable: ITR_RWEngine *m_pEngine; which is an abstract class and interface for the class that was created (RWEngine). Everything works perfectly fine, I load up the DLL, can execute the functions, etc.. with no problems. The only problem I run into is in the game client when I try to shut down the engine: FREEINTERFACE pfnFree = (FREEINTERFACE)::GetProcAddress( m_hEngineDll, "FreeRWInterface" ); if(pfnFree != 0) pfnFree( &m_pEngine ); // Release the DLL before shutting down. ::FreeLibrary( m_hEngineDll ); Now this seems to work fine (at least I believe it does) but the wierd thing is that when m_pEngine is deleted the destructor does not fire! I believe this has something to do with m_pEngine being a pointer to the RWEngine but of type interface and not the actual RWEngine (which is the destructor not firing). Let me know if there is anything I'm not doing, because RWEngine's destructor is NEVER called.
Advertisement
You need to declare the destructor as virtual in ITR_RWEngine.
E.g.
class ITR_RWEngine{public:   ITR_RWEngine() {}   virtual ~ITR_RWEngine() {}   // Other functions};
Thanks Evil Steve.

I wish I would have saw this earlier.. I couldn't figure out what the problem was and so I gutted my framework and have now changed everything over to using COM (which I am familiar with).

I do have one problem though right now.

I have a logfile class that inherits a singleton class. Right now neither of the destructors are virtual. I have everything working perfect in my engine except for when I destroy the main object the Logfile (singleton) class does not seem to fire the destructor at all. I create the logfile when I initialize my engine, like so:

HRESULT Initialize()
{
// blah blah
new CLogfile("engine.log");
// blah blah
}

and when I delete the engine object, the engine's destructor fires perfectly fine... but the logfile singleton's destructor does not. I am unsure what to do now, I have tried quickly changing the base singleton class destructor to virtual, but of course it did nothing.

Anybody know?
Do you call delete pLogfile (where pLogfile is the pointer to the CLogFile that you created with new)? If the object is created with new, it won't be freed until you call delete - even if the parent object has its destructor called.
Deleting the interface should be a function on the interface, which then does "delete this" in the implementation. That way, you don't have to worry about functions other than the interface factory function (and you could do ref counting, too).

Also, if you "delete this" in the implementation, then you don't actually need to worry about virtual destructors, because the type of "this" will be the most derived type.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement