Thread: Linked list / Octree deletion

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    166

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suspect that the problem is that you did not initialise the member pointers, so when val == 4 in FillTree(), the current TreeNode is left with member pointers that are uninitialised, but its destructor continues to try and delete them. Try initialising these member pointers to zero in the TreeNode constructor and see if that makes a difference. In case you have forgotten, it is safe to delete a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Quote Originally Posted by laserlight View Post
    I suspect that the problem is that you did not initialise the member pointers, so when val == 4 in FillTree(), the current TreeNode is left with member pointers that are uninitialised, but its destructor continues to try and delete them. Try initialising these member pointers to zero in the TreeNode constructor and see if that makes a difference. In case you have forgotten, it is safe to delete a null pointer.
    Sweet, that did the trick. I was hoping it was something simple. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM