I have two questions which are connected to each other.
I am implemeting a region quad-tree structure. In this QTree class, I have some fields and more importantly a
vector<QTree*> quads;
field that holds four divided regions of current square. Firstly, I want to implement a destructor for this class. Note that I cannot define anything in public field in the class (If I were, this was a piece of cake!) so I did this:
QTree::~QTree ()
{
if ( quads [ 0 ] != NULL )
{
for ( int i = 0; i < 4; i++ )
{
delete ( quads [ i ] );
}
}
}
According to valgrind, this works ie. no memory leak and error but I am not very sure if it is. Could you state your opinion about destructor's validity?
Second issue is that, in the implementation of overloaded assignment operator of this class if I write after self-assignment check and before anything else in this method this:
delete this;
I take
* Error in ` ': double free or corruption (out): 0x00007fffcb4d46d0 * : line 3: 4227 Aborted (core dumped) leakcheck
error.
If I write (same code in destructor.)
if ( quads [ 0 ] != NULL )
{
for ( int i = 0; i < 4; i++ )
{
delete ( quads [ i ] );
}
}
instead "delete this;" in overloaded assigment operator, I get no error. What is wrong, could you clarify me?
EDIT: I removed copy constructor.
Maybe the problem is using self destruction of an object. Here are some useful information on this: https://isocpp.org/wiki/faq/freestore-mgmt#delete-this