memory allocation and segmentation fault

Hi! I'm new to c++ (and forums), so I have some trouble with the memory management. I have a class Matrix which is basically only a class to manage a two dimentionnal array of integer named matrix. My destructor is :

1
2
3
4
5
6
7
Matrix::~Matrix()
{
    for(int i = 0; i < r; i++){
        delete[] matrix[i];
    }
    delete[] matrix;
}


I think it is good...but I'm not sure (if anybody can confirm it would be awesome). Then I have another class who use several matrices and even a vector of pointers of matrix that I instanciate with :

1
2
3
4
5
6
//create the vector
std::vector<Matrix*> v(0);
trees = v;   
//put a matrix in the vectr     
Matrix* tree = new Matrix(n,n);
trees.push_back(tree);


And then, the destructor of this class is :
1
2
3
4
5
6
7
Class1::~Class1()
{
    for(int k = 0; k < n/2; k++){
        matrices[k]->~Matrix();
    }
    matrices.~vector();
}


My first question is : Am I using destructors like I should ? Are they really doing what I expect?

My second is : I get a segmentation fault when I call ~Class1(). When I follow the program with the debugger, it seems to go all the way through the function and then stops when the function finish...the only information I get in my debugger is something with ntdll.dll FormatVirtualImage() in it (I googled it and didn't find anything). I'm using Code::blocks. Do anybody have any ideas what's happening?

Thank you so much for your time!

P.S. I'm sorry if I'm not in the right section...I think pointers, destructors and memory allocation were parts of the basics of c++..I hope I'm right!

Last edited on
Why are you explicitly calling the destructor for Matrix in the destructor for Class?

The Matrix destructor will be called automatically when the objects go out of scope.
My vector matrices is a private member variable of Classe1. I need to destroy it in the destructor, no ?
When a Class object goes out of scope it's destructor and the destrcutors of any objects it contains will automatically be called.

In Classes destrcutor you should be looping through the vector and deleting the pointers.
Last edited on
Ok...it makes sense. But then, when are we using destructors ? In cases like the class Matrix ?

I'm not sure what part of the code is good to post...

main instanciate an objet Class1 with :
Class1 c(arguments of class1);

I need to use several times Class1 with different parameters and, for now, I'm recreating the object each time and I'm trying to destroy it at the moment I don't need it anymore (I have a lot a data, so I need to save as much memory possible). Do you have any other idea ? Maybe I could use the same object and change parameters inside it, but then I will need to keep the matrices inside all the time and it takes too much space when the input is big.
Ok no no.. I was wrong, I can let my objects be destroyed when they go out of scope. I didn't know that class members are deleted when the object is.. I thought detructors were made to get rid of the parts of our object. Thank you for your answer, it's working now :)
Destrcutors are made to get rid of parts but only when those parts are allocated on the heap (pointers).

Otherwise it gets called behind the scenes and you don't need to worry about it.

You only need to call them explicitly when you have really complicated design or for testing purposes but this is very rare and usually unnecessary.
Topic archived. No new replies allowed.