Jump to content
  • Advertisement
Sign in to follow this  
mind in a box

Destructor isn't called

This topic is 2903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I have a list with blocks in my game. It is of the type "BasicBlock*", but also holds classes like LevelBlock* or TexturedBlock* and others which extend the BasicBlock class.

But when I call this:

void SceneManager::DeleteBlock(BasicBlock* Which)
{
list<BasicBlock *>::iterator it;
for(it=Blocks.begin();it!=Blocks.end();it++)
{
if((*it)==Which)
{
delete Which;
Blocks.erase(it);
break;
}
}
}



only the destructor from the BasicBlock-class gets called!

Here are two of my classes:
BasicBlock.h

#pragma once
#include "Direct2D_Vars.h"
#include "SceneManager.h"

class BasicBlock
{
public:
BasicBlock(void);
~BasicBlock(void);
//...



BasicBlock.cpp

#include "BasicBlock.h"


BasicBlock::BasicBlock(void)
{
MyClass=BASICBLOCK;
bSaveBlockInMapfile=true;
}


BasicBlock::~BasicBlock(void)
{
//This gets called
}
//...





LevelBlock.h

#pragma once
#include "BasicBlock.h"
#include "PhysicalComponent.h"

class LevelBlock : public BasicBlock
{
public:
LevelBlock(void);
~LevelBlock(void);
//...



LevelBlock.cpp

#include "LevelBlock.h"


LevelBlock::LevelBlock(void)
{
Position=D3DXVECTOR3(0,0,0);
Scale=D3DXVECTOR2(50,50);
Roundness=0;
RigidBody=NULL;
Rotation=0;
MyClass=LEVELBLOCK;
}


LevelBlock::~LevelBlock(void)
{
//This never gets called
Physics.DynamicsWorld->removeRigidBody(RigidBody);

delete RigidBody;
delete CollisionShape;
delete myMotionState;
}
//...




I don't think that is the normal behavior, or is it? I never noticed that before if so.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by mind in a box
That solved the problem, but it worked without it before many many times ...


No it didn't.

If you delete a pointer to a class without a virtual destructor, only the destructor for that specific class is called, which in turn calls the destructors of any parent classes.

If the pointer happens to actually point to a more derived class and the destructor is not virtual, the compiler has no way to know which destructor to start by calling.


class A
{
public:
~A(){ } // not virtual
};

class B
{
public:
~B(){ }
};

void f()
{
A *a=new B();
delete a; // only A::~A() will be called
}

Share this post


Link to post
Share on other sites
Ah, I believed my book here. I guess I have to update some code. [smile]
I never got any memory leaks and such stuff without using "virtual" with destructors. Maybe there was just some wired stuff going on :-/

Share this post


Link to post
Share on other sites
If you weren't hitting that destructor then the 'delete's in that constructor were never being called. Unless those pointers were already null, or deleted elsewhere, it seems like that could cause a memory leak.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!