Skip to main content
gamedev.net
🔒 Locked

Problem with virtual destructors

Started by Little Coding Fox Jun 18, 2007 at 6:48 AM 14 replies 2k views
Original Post
Little Coding Fox
Little Coding Fox
Hello everyone! I'm having quite a strange problem with my virtual destructors. I have a base class, IUnknown, which is just an empty class with a virtual destructor:

class IUnknown
{
protected:
	IUnknown() {};
public:
	virtual ~IUnknown() {};
};





And i have a derived class from that, a abstract class, called IGraphicsDevice.

class IGraphicsDevice : public IUnknown
...
virtual ~IGraphicsDevice() {};





And then have a derived class from IGraphicsDevice, OpenGLGraphicsDevice

class OpenGLGraphicsDevice : public IGraphicsDevice
...
virtual ~OpenGLGraphicsDevice();





However, even though the OpenGLGraphicsDevice destructor is called, the destructor of the class's members is not called for some reason. E.g., got a member that is a OpenGL Geometry Buffer which is derived from IGeometryBuffer which is derived from IUnknown aswell, and all destructors are virtual And "normal" classes as class members E.g.:

class TestShite
{
	unsigned int *d;
	TestShite()
	{
		d = new unsigned int;
	};
	~TestShite()
	{
		delete d;
	};

};



Still dont get their destructors called. I asked a few persons if they know what i should do, but they havent a clue... Also, their Constructors are called, but not their destructors. Anybody got any idea for what i should do? Sorry for the trouble, and have a nice day!
rip-off
rip-off
Are you sure that you are calling delete on them?

Other than that you will need to post more code, the complete OpenGLGraphicsDevice code(most importantly its constructors and destructor), and the code that supposedly destroys it.
joanusdmentia
joanusdmentia
This could happen if a type is incomplete at the point of deletion, this will prevent destructors from being called regardless of whether they're virtual. Your compiler should warn about it though. For example, the following will successfully compile but not call the destructor:
class MyClass;void func(MyClass* obj){    delete obj;}class MyClass{public:    ~MyClass()    {        std::cout << "MyClass::~MyClass() called" << std::endl;    }};
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
CipherCraft
CipherCraft
I might state the obvious here but are your members by any chance pointers? In that case there's no automatic delete for you; that's what the dtor is for.

hth,
CipherCraft
Little Coding Fox
Little Coding Fox
Sorry for taking so long to reply, had a bit of trouble logging in (kept getting timeouts..)

I'd show the whole OpenGLGraphicsDevice declaration and destructor, but it is a bit too big... So i'll just show a part of it:

	class OpenGLGraphicsDevice : public GhostEngine::IGraphicsDevice	{	private:		...		class TempShite		{		public:			unsigned int *d;			TempShite()			{				d = new unsigned int;			};			~TempShite()			{				delete d;			};		};		TempShite t; //Did this to try to see if its destructor is called, no luck		DynamicTemplatedPointer<OpenGLGeometryBuffer> GeoBuffers; //OpenGL Geometry Buffer class, tried to see if it would destroy itself but failed too	public:		...		virtual ~OpenGLGraphicsDevice() {}; //all variables should free themselves because of the destructors		...	}


None of my variables are pointers, they're either basic data types (uint, int, char, etc) or regular classes, but their destructors are never called.

I tried to break on
delete d;
, but it never hit that breakpoint, so its safe to assure that for some reason the destructors of the variables within OpenGLGraphicsDevice are never called.


Anybody have any ideas of what could be wrong?
yaustar
yaustar
How is the instance of OpenGLGraphicsDevice being created? On the stack or the heap?
Steven Yau
[Blog] [Portfolio]
Little Coding Fox
Little Coding Fox
Its created using new, so its on the heap.
(I create using new OpenGLGraphicsDevice, of course)
rip-off
rip-off
Quote:
Original post by nuno_silva_pt
Its created using new, so its on the heap.
(I create using new OpenGLGraphicsDevice, of course)


And are you 100% positive you are deleting this pointer correctly?

Are you using a smart pointer, or have you inserted a breakpoint on the line delete *name of variable*?
Little Coding Fox
Little Coding Fox
It calls its destructor (i tested to see if it breaks there when it should, and it does), but it doesnt call its member variables' Destructors.
yaustar
yaustar
It sounds VERY strange indeed. Have you tried a full clean and rebuild?
Steven Yau
[Blog] [Portfolio]
Little Coding Fox
Little Coding Fox
yeah, did a clean build, still the same happened. Dont know what's going on at all! :(
Little Coding Fox
Little Coding Fox
yeah, did a clean build, still the same happened. Dont know what's going on at all! :(
Little Coding Fox
Little Coding Fox
yeah, did a clean build, still the same happened. Dont know what's going on at all! :(
wack
wack
Quote:
Original post by nuno_silva_pt
It calls its destructor (i tested to see if it breaks there when it should, and it does), but it doesnt call its member variables' Destructors.


If you have member variables that are also allocated with new, you must call delete on them in (most likely) the destructor of the class they are members of.
Little Coding Fox
Little Coding Fox
Sorry for taking so long and for triple posting, but i'm having trouble with the site...

Anyhow, i think i figured out what's wrong.

On my memory pool, where i delete all my objects, one of the objects gives me a access violation, but VS 2005 doesnt warn me like it should, so the program just quits.

I found this out while re-checking the debug log, but still i find it weird that the debugger doesnt show me the error...

Anybody got any idea on what i should do to make it break on that error? That's the only thing stopping me from fixing this!

Sorry for the trouble, again...
Antheus
Antheus
Quote:
On my memory pool, where i delete all my objects, one of the objects gives me a access violation, but VS 2005 doesnt warn me like it should, so the program just quits.


Oh, wow.

Memory pool isn't something you mention last.

Look at documentation on new. It'll clearly state that in some cases, destructors aren't called.

Show the actual code, memory pool, allocations, constructors, and the rest.

PS: The site has been buggy the whole day

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.