C++ destructor seems to be called 'early'
Posted
by
suicideducky
on Stack Overflow
See other posts from Stack Overflow
or by suicideducky
Published on 2010-12-28T22:16:59Z
Indexed on
2010/12/29
3:54 UTC
Read the original article
Hit count: 331
Please see the "edit" section for the updated information.
Sorry for yet another C++ dtor question... However I can't seem to find one exactly like mine as all the others are assigning to STL containers (that will delete objects itself) whereas mine is to an array of pointers.
So I have the following code fragment
#include<iostream>
class Block{
public:
int x, y, z;
int type;
Block(){
x=1;
y=2;
z=3;
type=-1;
}
};
template <class T> class Octree{
T* children[8];
public:
~Octree(){
for( int i=0; i<8; i++){
std::cout << "del:" << i << std::endl;
delete children[i];
}
}
Octree(){
for( int i=0; i<8; i++ )
children[i] = new T;
}
// place newchild in array at [i]
void set_child(int i, T* newchild){
children[i] = newchild;
}
// return child at [i]
T* get_child(int i){
return children[i];
}
// place newchild at [i] and return the old [i]
T* swap_child(int i, T* newchild){
T* p = children[i];
children[i] = newchild;
return p;
}
};
int main(){
Octree< Octree<Block> > here;
std::cout << "nothing seems to have broken" << std::endl;
}
Looking through the output I notice that the destructor is being called many times before I think it should (as Octree is still in scope), the end of the output also shows:
del:0
del:0
del:1
del:2
del:3
Process returned -1073741819 (0xC0000005) execution time : 1.685 s
Press any key to continue.
For some reason the destructor is going through the same point in the loop twice (0) and then dying.
All of this occures before the "nothing seems to have gone wrong" line which I expected before any dtor was called.
Thanks in advance :)
EDIT The code I posted has some things removed that I thought were unnecessary but after copying and compiling the code I pasted I no longer get the error. What I removed was other integer attributes of the code. Here is the origional:
#include<iostream>
class Block{
public:
int x, y, z;
int type;
Block(){
x=1;
y=2;
z=3;
type=-1;
}
Block(int xx, int yy, int zz, int ty){
x=xx;
y=yy;
z=zz;
type=ty;
}
Block(int xx, int yy, int zz){
x=xx;
y=yy;
z=zz;
type=0;
}
};
template <class T> class Octree{
int x, y, z;
int size;
T* children[8];
public:
~Octree(){
for( int i=0; i<8; i++){
std::cout << "del:" << i << std::endl;
delete children[i];
}
}
Octree(int xx, int yy, int zz, int size){
x=xx;
y=yy;
z=zz;
size=size;
for( int i=0; i<8; i++ )
children[i] = new T;
}
Octree(){
Octree(0, 0, 0, 10);
}
// place newchild in array at [i]
void set_child(int i, T* newchild){
children[i] = newchild;
}
// return child at [i]
T* get_child(int i){
return children[i];
}
// place newchild at [i] and return the old [i]
T* swap_child(int i, T* newchild){
T* p = children[i];
children[i] = newchild;
return p;
}
};
int main(){
Octree< Octree<Block> > here;
std::cout << "nothing seems to have broken" << std::endl;
}
Also, as for the problems with set_child, get_child and swap_child leading to possible memory leaks this will be solved as a wrapper class will either use get before set or use swap to get the old child and write this out to disk before freeing the memory itself.
I am glad that it is not my memory management failing but rather another error. I have not made a copy and/or assignment operator yet as I was just testing the block tree out, I will almost certainly make them all private very soon.
This version spits out -1073741819.
Thank you all for your suggestions and I apologise for highjacking my own thread :$
© Stack Overflow or respective owner