I have a class Aclass
which contains a pointer to struct GraphStructure *Graph_
.
The Aclass
constructor initializes this pointer by dynamically allocating a GraphStructure
instance.
The GraphStructure
instance also dynamically allocates arrays and calls delete on these in its destructor.
The order in which the destructors are called are (1) the class destructor and (2) the struct destructor.
If I call delete
on Graph_
in the Aclass
destructor and then the destructor for the struct is called, is there a double free happening here as my program stops responding.
If I don't call delete Graph_;
then it runs ok but am I not introducing a memory leak here?
What is the correct way to handle this?
I've tried to debug the program.
struct GraphStructure
{
idx_t* xadj;
idx_t* adjncy;
GraphStructure() {
xadj = new idx_t[5];
adjncy = new idx_t[5];
}
~GraphStructure() {
delete[] xadj;
delete[] adjncy;
}
};
class Aclass
{
GraphStructure *Graph_;
}
Aclass::Aclass():Graph_(new GraphStructure()) {}
Aclass::~Aclass()
{
//delete Graph_;
}