• Advertisement
Sign in to follow this  

Error with array of pointers in scene graph

This topic is 2677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm creating a 2D scene graph for a game, and have run into errors when trying to free the memory. Sorry for "code dumping" in this forum but I've thrown all I've got at this problem already. I've thinned it down to the bare minimum of code and debugged to try and pin the problem down, without success. Obviously there is some basic aspect of pointer management or constructor/destructor behaviour that I haven't grasped yet.

What I do know is that:
1) No errors occur if the default constructor is never called.
2) Before self-destructing, each node first calls the destructors of its two child nodes. This process fails after the leaf node, where according to the debugger the array of two children is itself somehow set to NULL. Does this mean that the expression children = NULL; is bad practice?

The code (spatial_node.h):
#ifndef SPATIAL_NODE_H
#define SPATIAL_NODE_H

#include <windows.h>

struct SpatialNode
{
SpatialNode *children[2]; // pointers to both children, if not a leaf node
int tempLevel;

SpatialNode();
SpatialNode(unsigned level); // a level 5 node contains two level 4 nodes - 1 is the leaf level
~SpatialNode();
};

SpatialNode::SpatialNode()
{
for (int i = 0; i < 2; ++i)
children = NULL;

tempLevel = 0;
}

SpatialNode::SpatialNode(unsigned level)
{
for (int i = 0; i < 2; ++i) // iterate twice
children = NULL; // set each pointer to NULL

tempLevel = level;

if (level > 1) // if this is not a leaf node
for (int i = 0; i < 2; ++i) // iterate twice, for each iteration create one child node
children = new SpatialNode(level - 1);
}

SpatialNode::~SpatialNode()
{
// delete both of the child nodes
for (int i = 0; i < 2; ++i)
delete children; // deleting a NULL pointer is safe
}

#endif // SPATIAL_NODE_H


In "main.cpp" all I have to do to reproduce the error is to first declare the scene graph with its default constructor and then initialise it, like so:

SpatialNode spatialTree;
spatialTree = SpatialNode(8);


Could someone please explain what I've got wrong here? Thanks for your time and attention.

Share this post


Link to post
Share on other sites
Advertisement
Your class doesn't implement the rule of three. You're getting double deletions.

In your sample code:

SpatialNode spatialTree;
spatialTree = SpatialNode(8); // <-- Assignment, not initialisation

To initialise:

SpatialNode spatialTree(8);
// Or
SpatialNode spatialTree = SpatialNode(8);

On a stylistic note, #including windows.h is in no way minimal [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Your class doesn't implement the rule of three. You're getting double deletions.


Brilliant, thanks for that. I wrote a copy constructor and assignment operator which both set the pointers in the original instance to NULL after copying them. It works, but something tells me it's not the best of solutions, so I'll try and figure out a way of restructuring it.

Quote:
On a stylistic note, #including windows.h is in no way minimal [smile]


True, I should have said easy to read. [smile] I wanted the NULL macro quickly so I just threw in 'windows.h' and then forgot about it. It's also defined in 'stdio.h' so I could have used that. The main project uses SDL and should be compilable on any platform.

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
Quote:
I wanted the NULL macro..


#ifndef NULL
#define NULL 0
#endif


Yes now I feel stupid...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement