Composition with pointers

Jul 1, 2010 at 5:18pm
I created a class that has as members two pointers to other classes I created. I am having problems with the destructor of this class. Because the members are pointers, I try to use delete in the destructor, but the error does not go away. I already tried not to do anything in the destructor.

What is the correct procedure for such a case?

Thank you in advance,
L.C.
Jul 1, 2010 at 5:29pm
llcaldeira wrote:
Because the members are pointers, I try to use delete in the destructor

Are these pointers you try to delete created with new? If not, you should not delete them. Another problem could be that you delete these pointers twice in your program...

EDIT: In genaral, when you use aggregation (i.e. composition with pointers), the containing class is not responsible for the creation/destruction of the contained objects.
Last edited on Jul 1, 2010 at 5:31pm
Jul 2, 2010 at 8:19am
Thanks for your fast response.

The members that are pointers are declared in the header file and created with new in the constructor of the composition class.

The objects that are pointed should not exist after the composition class has been destroyed and that is one of the reasons why I try to delete them, otherwise they will be lost somewhere, right? (You are right, in a pure aggregation objects can still exist.)

I never use delete except in the destructor of the composition class. But actually the mistake I have seems that something is being freed twice and that is a bad mistake!

Thank you for any advice you might give me.
L.C.
Jul 2, 2010 at 8:45am
Have you created your own copy constructor and operator = for your composition class?
Jul 2, 2010 at 9:04am
No.... Do I really have to do it?

I'm posting here my problem:

class MixedPrior: public Prior_abstract
{
public:

//---methods:

//---constructor
MixedPrior(Image3D* img);

//---destructor
~MixedPrior();

private:
//---data members:

QPrior *qpriorimg;
GaussPrior *gausspriorimg;
};


MixedPrior::MixedPrior(Image3D* img) : Prior_abstract(img)
{
this->qpriorimg=new QPrior(img);
this->gausspriorimg=new GaussPrior(img);
}

MixedPrior::~MixedPrior(void)
{
cout << "I am in the Mixed Prior Destructor! " << endl;
this->qpriorimg=NULL;
this->gausspriorimg=NULL;
delete this->qpriorimg;
delete this->gausspriorimg;
}


{
Mixedprior=new MixedPrior(image_ref);
...
}
Jul 2, 2010 at 9:30am
It would be a good idea to, but if you are only using your constructor to create instances, you could go ahead and make the = and copy constructor private (so they can't be called).

Anyway, your issue is you are NULLing the pointers before you are deleting them.
Jul 2, 2010 at 1:01pm
As a general rule, if you have to free memory in your destructor either with delete or free(), then
you need to write an appropriate copy constructor and assignment operator, otherwise you have
to make the class non-copyable (declare copy constructor and assignment operator in the private
section and leave them unimplemented).
Topic archived. No new replies allowed.