I have a little trouble with a destructor. In its current state, it create a segfault. Please note that the destructor is only implemented and never explicitly called anywhere. The segfault appear no matter where the breakpoints are.
Here is the destructor :
Graph::~Graph() {
while(!children.empty()) {
for(vector<Graph*>::iterator itr = children.begin(); itr != children.end(); ++itr) {
delete *itr;
}
children.clear();
delete parent;
delete tab;
}
}
I also did a variation like this, without better results :
Graph::~Graph() {
while(!children.empty()) {
for(unsigned i = 0; i < children.size(); i++)
{
delete children.at(i);
}
children.clear();
delete parent;
delete tab;
}
}
Here is the class declarations :
class Graph
{
private :
Graph* parent;
vector<Graph*> children;
Board* tab;
public :
Graph(Board);
Graph(Board, Graph*);
~Graph();
void AddNode(Board&);
// Graph& BFS(Graph&);
Graph& operator=(Graph source);
vector<Graph*>& getchildren();
Graph* getparent();
Board* gettab();
};
class Board {
private :
int** tab;
int nbline;
int nbcolumn;
Position emptyspot;
public :
Board();
Board(int, int, Play&);
Board(int, int);
Board(const Board&);
Board(int, int, ifstream&);
~Board();
void setValue(Position&, int);
void setNbline(int m);
void setNbcolumn(int n);
int getValue(Position&);
int getNbline();
int getNbcolumn();
int getEmptyline();
int getEmptycolumn();
void setEmptySpot(Position&);
Position& getEmptySpot();
Board& operator=(Board& source);
};
Board::~Board()
{
for(int i = 0; i < this->nbline; i++)
{
delete tab[i];
}
delete tab;
}
I'm not really comfortable and very inexperienced with debugger so i don't really know how to use it properly. The call stack point at this line is stl_vector.h :
/**
* Returns a read-only (constant) iterator that points one past
* the last element in the %vector. Iteration is done in
* ordinary element order.
*/
const_iterator
end() const _GLIBCXX_NOEXCEPT
{ return const_iterator(this->_M_impl._M_finish); }
I don't know what these lines mean to be honest.
The call stack also show the while loop line in the debugger, with the note : Graph::~Graph(this=0x90909090, __in_chrg=optimized out). I also point 3 times the line delete *itr (with the same note).
So my question is, how can i destruct my Graph object ? :'(
EDIT : after further experimentations, the segfault disappear when i delete the only linein code that add things in the vector. Here is the method. I'll add that the values in the vector are always the sames (shouldn't be).
void Graph::AddNode(Board& tablo)
{
Graph tmp(tablo, this);
Graph* newChild = &tmp;
children.push_back(newChild); // if i commend this, no segfault
}
I don't know if this is two different problems or the push_back is the cause of the destructor malfunction. I think it's non-related, i expected the segfault to disappear (of course destructor don't have trouble to destruct the tree if the tree only got one node).
EDIT2 : This code doesn't create any segfault, but it doesn't truly destruct all the vectors in the vectors, right ? I think it doesn't because erase will just destroy pointers and not the objets themselves.
while(!children.empty()) {
children.erase(children.begin(),children.end());
delete parent;
delete tab;
}
Moreover, with this, sometimes the program seem to execute well but don't stop at the end of execution. Debbuger doesn't seem to find anything
EDIT : as ask, the copy constructor of Graph :
Graph::Graph(const Graph& source) {*this = source;}
Graph& Graph::operator=(Graph source)
{
if(this!=source)
{
this->parent = source.parent;
this->tab = source.tab;
// this->seen = source.seen;
while(!source.children.empty()) {
for(unsigned i = 0; i<source.children.size(); i++) {
this->children.at(i) = source.children.at(i);
}
}
}
return *this;
}