Burak Dağlı
Burak Dağlı

Reputation: 512

Segmentation fault in destructor

I get a segmentation fault when I try to execute my project. At the end of main where the destructor of the Btree class is run it calls the destructor of the Node class. Then in the destructor call of the Word class I get the error. And list.tcc opens (~Btree -> ~Node() -> ~Word() (error): list.tcc:)

Cursor gives error in this line:

list.tcc:

_Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);

Is the reason of this error the list in the Word class?

The classes's codes abbreviated as below:

class Btree{
private:
...
Node *root;
...
public:...
~Btree(){delete[] root;};

};

class Node{
...
Word *words;
Node **children;
...
    ~Node(){delete [] words; delete []children;};
};

class Word{
public:
string word;
list<Couple> couple;

    Word(){};
    ~Word(){};
};

class Couple{
...
public:
....
    ~Couple(){};
 };

Upvotes: 0

Views: 1483

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153802

How did you allocate your root? My personal guess would be that you allocate it using

root = new Node();

If you try to deallocate a non-array object as an array object, you will get undefined behavior. Practically, it would take the word right before the start of your node and assume it is a count of elements and destroy that number of elements. Since there is just one this bound not to work too well. You probably want

delete root;

Upvotes: 1

Related Questions