Linked list / Octree deletion

This is a discussion on Linked list / Octree deletion within the C++ Programming forums, part of the General Programming Boards category; Hi, I have made this octree for use with intersection code later. I can subdivide the tree fine and intersect ...

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:

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.

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!