Page 1 of 1

Retrieving Pointer From smgr->AddMeshSceneNode call

Posted: Fri May 14, 2010 12:11 am
by so1odo1o
I currently have a class that holds a pointer to an IMeshSceneNode, found below:

Code: Select all

class GraphNode
{
public:
	GraphNode();
	scene::IMeshSceneNode* node;
};
The problem is that when I create a GraphNode and do something like this:

Code: Select all

GraphNode a;
a.node = smgr->addMeshSceneNode(//node properties..);
and then try something like:

Code: Select all

cout << a.node->getPosition().X << endl;
This fails at runtime. After looking through the a.node properties, it doesn't seem to hold the information for the node that was added. Am I doing something wrong here? Please let me know. Thank you.

Posted: Fri May 14, 2010 12:17 am
by Seven
can you post a small app that shows the issue?

Posted: Fri May 14, 2010 12:36 am
by so1odo1o
The program that I have is a bit long. But if I had created a class that holds a pointer to an IAnimatedMeshScene, shouldn't the above code work? I set the pointer in my class to the pointer that is returned from the smgr above.

When I run the program, it says in the debugger that the pointer node in my class is 0xcccccccc which makes the program crash.

Weird. The code below works but not the one outside the loop:

Code: Select all

	for(int i = 0; i < num; i++)
	{
		GraphNode gn = nodes.at(i);
		gn.node = smgr->addMeshSceneNode(meshes[0], 0, gn._id, core::vector3df(gn._x,gn._y,gn._z));
		cout << gn.node->getPosition().X << endl;
	}

	cout << nodes.at(1).node->getPosition().X << endl;

Posted: Fri May 14, 2010 12:39 am
by bitplane
Are you passing those graphnodes around, causing them to be copied, without having a default value for node or a copy constructor?

Posted: Fri May 14, 2010 7:17 am
by vitek
so1odo1o wrote: Weird. The code below works but not the one outside the loop:

Code: Select all

	for(int i = 0; i < num; i++)
	{
		GraphNode gn = nodes.at(i);
		gn.node = smgr->addMeshSceneNode(meshes[0], 0, gn._id, core::vector3df(gn._x,gn._y,gn._z));
		cout << gn.node->getPosition().X << endl;
	}

	cout << nodes.at(1).node->getPosition().X << endl;
Actually, it is not weird at all.

Code: Select all

GraphNode gn = nodes.at(i);
gn.node = smgr->addMeshSceneNode(...);
The first line creates a local copy of a GraphNode, and the second line updates it. When the loop iteration is finished, the copy goes out of scope and is destroyed. The original item in the collection is not updated. Then, when you get outside the loop, you try to access the element at index 1. Assuming that there are at least two elements in the collection, your code doesn't work because the collection was not updated inside the loop.

The easy thing to do is to use a reference to the element in the node collection instead of making a copy of the element.

Code: Select all

GraphNode& gn = nodes.at(i);
gn.node = smgr->addMeshSceneNode(...);
Travis