Getting seg faults in destructors. Possibly a newbie problem.

A

Andrew King

Hi everyone,

I've got a problem with segmentation faults occurring in the
destructor of an object that I have. The only thing that I'm deleting
is an array. The destructor is

Experiment::~Experiment(){
if(ClusteringS) delete [] ClusteringS;
ClusteringS = NULL;
}

and the array is allocated in

void Experiment::ClusteringSAlloc(Graph& graph)
{
ClusteringS = new int [graph.Order];
}

Other than that, there's nothing unusual going on with the array. All
the calls have the flavour of

ClusteringS = graph.WhichCluster;

The seg fault doesn't always happen, either. Seems to be about half
the time. Any ideas? I'm kind of new to trying to use destructors
properly, so it could be something kind of easy, but I suspect not.
 
J

John Harrison

Andrew King said:
Hi everyone,

I've got a problem with segmentation faults occurring in the
destructor of an object that I have. The only thing that I'm deleting
is an array. The destructor is

Experiment::~Experiment(){
if(ClusteringS) delete [] ClusteringS;
ClusteringS = NULL;
}

Its simpler to say

Experiment::~Experiment(){
delete [] ClusteringS;
}

There no need either to test for NULL before a delete (deleting NULL does
nothing) or to set to NULL after the delete (since you're are in the
destructor you don't care what the value of ClusterS is any more).

But this isn't the answer to your problem.

and the array is allocated in

void Experiment::ClusteringSAlloc(Graph& graph)
{
ClusteringS = new int [graph.Order];
}

Other than that, there's nothing unusual going on with the array. All
the calls have the flavour of

ClusteringS = graph.WhichCluster;

The seg fault doesn't always happen, either. Seems to be about half
the time. Any ideas? I'm kind of new to trying to use destructors
properly, so it could be something kind of easy, but I suspect not.


It could be lots of things.

Maybe you are copying your Experiment object but you haven't written a copy
constructor or assignment operator. Maybe you aren't aware of the issues
surrounding these, if so you should acquaint yourself pretty quickly.

Maybe you have heap corruption, which generally shows up during a delete.
The cause of that could be any number of bugs in your program.

Since the problem is sporadic I'd be inclined to say the later, but without
seeing all the code it's impossible to say.

You should also learn up on vectors and other STL data types. It's to avoid
problems exactly like yours that these are recommended. Any decent C++ book
will at least mention these.

john
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top