Linked list / Octree deletion
Hi, I have made this octree for use with intersection code later. I can subdivide the tree fine and intersect it but when I call delete on the root node of the tree I get a crash. Can you tell me what the issue may be by looking at my code? Calling delete on the rootnode is supposed to delete all the children in the childrens destructors. Here is all code dealing with creating the octree:
Code:
class BoxVolume //this is a volume unit in the octree
{
public:
float minx,miny,minz,maxx,maxy,maxz;
BoxVolume(float mix, float miy, float miz, float max, float may, float maz)
{
minx = mix; miny = miy; minz = miz;
maxx = max; maxy = may; maxz = maz;
}
};
class TreeNode //This is what linked together in the list, each node has 8 children
{
public:
TreeNode(BoxVolume *box) {b = box;}
~TreeNode();
BoxVolume *b;
TreeNode *next1;
TreeNode *next2;
TreeNode *next3;
TreeNode *next4;
TreeNode *next5;
TreeNode *next6;
TreeNode *next7;
TreeNode *next8;
};
TreeNode::~TreeNode() //Supposed to delete all children of the node, some issue here...
{
delete b; b = 0;
delete next1; next1 = 0;
delete next2; next2 = 0;
delete next3; next3 = 0;
delete next4; next4 = 0;
delete next5; next5 = 0;
delete next6; next6 = 0;
delete next7; next7 = 0;
delete next8; next8 = 0;
}
TreeNode *volumeTree; //Root node
void FillTree(TreeNode *t, int val) //Function for recursively creating the octree
{
if (val == 4) return; //depth of octree is reached
//Create the new sub box volumes
float minx = t->b->minx, maxx = t->b->maxx;
float miny = t->b->miny, maxy = t->b->maxy;
float minz = t->b->minz, maxz = t->b->maxz;
float midx = (minx+maxx) * 0.5f;
float midy = (miny+maxy) * 0.5f;
float midz = (minz+maxz) * 0.5f;
BoxVolume *box1 = new BoxVolume(minx, miny, minz, midx, midy, midz);
BoxVolume *box2 = new BoxVolume(minx, midy, minz, midx, maxy, midz);
BoxVolume *box3 = new BoxVolume(midx, miny, minz, maxx, midy, midz);
BoxVolume *box4 = new BoxVolume(midx, midy, minz, maxx, maxy, midz);
BoxVolume *box5 = new BoxVolume(minx, miny, midz, midx, midy, maxz);
BoxVolume *box6 = new BoxVolume(minx, midy, midz, midx, maxy, maxz);
BoxVolume *box7 = new BoxVolume(midx, miny, midz, maxx, midy, maxz);
BoxVolume *box8 = new BoxVolume(midx, midy, midz, maxx, maxy, maxz);
//Now we have the volumes of the subboxes of the box passed in
t->next1 = new TreeNode(box1);
t->next2 = new TreeNode(box2);
t->next3 = new TreeNode(box3);
t->next4 = new TreeNode(box4);
t->next5 = new TreeNode(box5);
t->next6 = new TreeNode(box6);
t->next7 = new TreeNode(box7);
t->next8 = new TreeNode(box8);
FillTree(t->next1,val+1);
FillTree(t->next2,val+1);
FillTree(t->next3,val+1);
FillTree(t->next4,val+1);
FillTree(t->next5,val+1);
FillTree(t->next6,val+1);
FillTree(t->next7,val+1);
FillTree(t->next8,val+1);
}
void CreateTree() //Pseudocode for tree initialization
{
//First we have calculated the min and max values of x,y,z (bounding box of 3d object)
BoxVolume *box = new BoxVolume(minx, miny, minz, maxx, maxy, maxz);
volumeTree = new TreeNode(box);
FillTree(volumeTree,0);
delete volumeTree; //This is were the crash comes
}
Thanks for looking:)