Sign in to follow this  
Damocles

Deleting child classes when the pointer is a parent class type?

Recommended Posts

I have an issue where I have several pointers to child class objects of varying, subclassed types and I need to destroy the data of these children, but I only have parent class pointers. That sounds confusing, so to try and explain: I have a base class, let's call it Foo. I have multiple subclasses, let's call them Bar1, Bar2, Bar3 ... BarN, etc. I create an instance of Foo, and this is my "parent" object. I then create instances of Bar1, Bar2, Bar3, etc and pass a Foo* pointer back to the Foo parent object (for each Bar instance) so that I might track what children instances have been instantiated. The problem is, when Foo is deleted, it also needs to delete all the child classes, but I don't know specifically what type they are - I only have a Foo* pointer for them. Deleting just the objects pointed to by Foo* pointers creates memory leaks because not all the BarN class data is being deleted. I considered adding identifier variables into the Bar1,2,3,etc classes, but that seems a very clumsy way of handling it and will result in a ton of extra coding as the number of types of Bar increases. So what I need is some clever way to destroy the Bar class objects when all I have are Foo* pointers. I had the idea that when I pass along the Foo* pointer to the parent object, I could also pass along the size of the BarN class, then manually destroy the data between Foo* and Foo*+size. I think this is risky though as I'm not 100% sure if the data in subclasses will store all the parent class and subclass data in a continuous block or not. So I figure there's probably some clever trick that everyone but me knows about for dealing with situations like this. Anyone want to show me the way?

Share this post


Link to post
Share on other sites
Make the superclass (Foo) destructor virtual, and you can just delete the pointers as usual (hopefully you know about virtual methods :P).

Share this post


Link to post
Share on other sites
You can do that? Why am I only hearing about this now!? :)

Thanks.

edit: No wait, I just checked and my destructors are virtual. VS sets them to virtual by default. Hmm, I guess the memory leaks must be being caused by something else then...

Edit 2:
This is very odd. I originally thought the problem was with child classes not being deleted because the memory leak detection routines were flagging the moment I instatiate those child classes as where the leak originates. After some digging I find only some of the child classes are causing leaks, and the common ground between them is use of this struct:


struct SColor{ // 4 float color struct
float r;
float g;
float b;
float a;
SColor()
{
r=1.0f;
g=1.0f;
b=1.0f;
a=1.0f;
}
SColor(float r1, float g1, float b1, float a1)
{
r=r1;
g=g1;
b=b1;
a=a1;
}
};



I only ever use scope instances of that struct, never a pointer/memory allocation. And all the uses of the struct are used as class members, so they should be getting destroyed along with the class, but it seems like it's this struct causing the problem. It looks okay to me, but maybe I'm missing something that aids in the destruction of it. I'm also curious as to why the debug routines are flagging the call to "x = new BarN;" as the problem and not the instatiation of the struct.

[Edited by - Damocles on May 21, 2006 6:23:03 AM]

Share this post


Link to post
Share on other sites
Visual Studio sets your destructors to virtual by default?

That doesn't seem correct.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by _goat
Visual Studio sets your destructors to virtual by default?

That doesn't seem correct.

I figure he means that the class wizard or whatever that gizmo that adds classes and methods for you is does it automatically.

Share this post


Link to post
Share on other sites
Yeah, I meant the class wizard - I can't remember the last time I made a class by hand :)

In other news - I fixed the problem, but I don't have a clue what the problem was. I eventually traced the problem back to the point where a parent object deletes all it's child objects. Although I couldn't find anything wrong with it, I had been meaning to switch it over from using arrays of pointers to using a std::vector of pointers. So I did, and after doing this the problem disappeared. Most perculiar.

[Edited by - Damocles on May 21, 2006 8:11:24 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this