Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

destruction of dynamic arrays 1

Status
Not open for further replies.

pipk

Programmer
Feb 20, 2001
455
GB
Dear Friends

Sorry if this is an inane and basic question, it just reflects the ignorance of the writer if it is...

I have created two class files, one for reading Greyscale type pgm files and consists of a dynamically allocated 2 dimensional array. For which the destructor seems to work fine.

My second class file is for reading colour Images and contains 3 greyscale objects (one for each colour channel RGB).

My question is, do i need a separate destructor to remove colour images, or will the grey scale destructor be called automatically?

I currently have a colour image destructor that tries to delete my greyscales (i.e. invoke their destructors) but i am getting a segmentation fault and a core dump.

Once again, sorry if this is a stupid question.

PipK (learner!!)



, the other is a colour image file
 
Whenever your greyscale objects go out of scope, the constructor will be called. A good rule of thumb is that if a class contains the statement: new, it needs a corresponding delete for each new.

Therefore, if you have:
ColorImage::ColorImage(){
RGB_I *an_image = new RGB_I();
/* You need to delete the memory */
}

but, with
ColorImage::ColorImage(){
RGB_I an_image = RGB_Init();
}
You don't need one, the destructor for RGB_I will take care of it.

When you get a seg fault, chances are you're deleting something you still need or you're deleting the wrong thing. If you post that, we'll take a look.
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Each member is declared in my header file as:

Grey_Scale *Red; etc

The following is my colourimage constructor:

Colour_Image::Colour_Image(string c, long d, long e, long f)
{
Red = new Grey_Scale("Red", d, e, f);
Green = new Grey_Scale("Green", d, e, f);
Blue = new Grey_Scale("Blue", d, e, f);
}

Each Grey_scale object is an unsigned char**

I have tried the following methods of deletion (more out of frustration than anything else).

Colour_Image::~Colour_Image()
{

delete [] Red; //etc
//This causes seg fault.

delete Red; //etc
//as above.

delete *Red; //etc
// won't compile as it is expecting a //pointer.
}

Hope you can help.

Cheers

pipk
 
The 2nd one should work right, have you verified your destructor for greyscale images works correctly, post it here... I think that may be the problem.
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Here is the grey desytructor. I was hoping it cleared out the array by column within row.

Grey_Scale::~Grey_Scale()
{
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
delete[] *(array+j);

delete[] array;
}
 
Actually I think I have sussed it. I should
be doing this instead.

Grey_Scale::~Grey_Scale()
{
for(int j = 0; j < height; j++)
delete[] *(array+j);

delete[] array;
}

It has got rid of the seg fault.
Many thanks for bringing it to my attention though - no wonder you keep making tipster of the week.

cheers

pipk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top