Deconstructor fails to clear everything

S

steflhermitte

Dear cpp-ians,

I have a class:

class mImage
{
public:
//constructors / destructor
mImage(unsigned int nrLayers);
~mImage();
//member functions
...

private:
//data container
unsigned int NbLayers;
vector<Image *> vectorImage;
vector<Image *>::iterator itVectorImage;

//data clearing
void clear();

};

and I have problems with me deconstructor:

mImage::~mImage()
{
clear();
}

void mImage::clear(void)
{
if(NbLayers>0)
{
for(unsigned int l=0;l<NbLayers;++l)
{
delete vectorImage[l];vectorImage[l]=NULL;
}
vectorImage.clear();
}
NbLayers=0;
}

The problem is that apparently the command
"delete vectorImage[l];vectorImage[l]=NULL;"
does not clear the images stored in it, but only the vector itself.

vectorImage is a vector<Image *> and for the class Image I have a
deconstructor command that works perfectly.

Any advice how I should clear the vectorImage together with all the
images that are in it?

Kind regards and thank you in advance,
Stef
 
R

Rolf Magnus

steflhermitte said:
Dear cpp-ians,

I have a class:

class mImage
{
public:
//constructors / destructor
mImage(unsigned int nrLayers);
~mImage();
//member functions
...

private:
//data container
unsigned int NbLayers;
vector<Image *> vectorImage;
vector<Image *>::iterator itVectorImage;

//data clearing
void clear();

};

and I have problems with me deconstructor:

mImage::~mImage()
{
clear();
}

void mImage::clear(void)
{
if(NbLayers>0)
{
for(unsigned int l=0;l<NbLayers;++l)
{
delete vectorImage[l];vectorImage[l]=NULL;
}
vectorImage.clear();
}
NbLayers=0;
}

The problem is that apparently the command
"delete vectorImage[l];vectorImage[l]=NULL;"
does not clear the images stored in it, but only the vector itself.

What do you mean by "apparently"? What makes you believe it doesn't work?
vectorImage is a vector<Image *> and for the class Image I have a
deconstructor command that works perfectly.

Any advice how I should clear the vectorImage together with all the
images that are in it?

What you are doing looks fine, unless NbLayers has the wrong value. Btw, you
don't need that member anyway. vectorImage.size() will give you the number
of elements in the vector.
 
S

steflhermitte

Looking at the size of my Page File History, the memory is not cleared
sufficiently.

I thought it was because of the assignment of vectorImage:

void mImage::setImages()
{
itInputFileName = inputFileName.begin();

for (itVectorImage = vectorImage.begin(); itVectorImage!=
vectorImage.end(); itVectorImage++ )
{
Image *tempImage = new Image(*(itInputFileName));
*itVectorImage = &(*tempImage);
itInputFileName++;
}
}

vectorImage is thus a vector of pointers to images. When I delete
vectorImage, I was afraid the images were not cleared simultaneously.

Thanks for your help!

Stef
 
K

Kristo

steflhermitte said:
Looking at the size of my Page File History, the memory is not cleared
sufficiently.

Your destructor code in an earlier post looked ok. Just because the
memory isn't freed back to the OS doesn't mean it isn't freed for later
use by the program itself. If you're worried about memory leaks, there
is software out there to help you with that, e.g., purify.
I thought it was because of the assignment of vectorImage:

void mImage::setImages()
{
itInputFileName = inputFileName.begin();

for (itVectorImage = vectorImage.begin(); itVectorImage!=
vectorImage.end(); itVectorImage++ )
{
Image *tempImage = new Image(*(itInputFileName));
*itVectorImage = &(*tempImage);

You don't need a temporary here. You can do it in one line:
*itVectorImage = new Image(*itInputFileName);
itInputFileName++;
}
}

You're right. This function might be a problem if vectorImage already
contains valid pointers to images. You aren't deleting the old
pointers before overwriting them. In that case, the memory they used
to point to will be leaked.
vectorImage is thus a vector of pointers to images. When I delete
vectorImage, I was afraid the images were not cleared simultaneously.

Assuming vectorImage already contains valid pointers, you need the
line...

delete *itVectorImage;

....in your setImage() function before assigning a new object.

Kristo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top