remove scenenode in another class

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Kazooie
Posts: 13
Joined: Sat Jan 17, 2009 4:26 pm

remove scenenode in another class

Post by Kazooie »

I made a class called CUnit which holds an IAnimatedMeshSceneNode as a public variable. To try to find the problem I got rid of all the other functions and had it load a model during construction. When I use the remove function, which simply calls node->remove(), the program crashes.

When I write the same exact code in the main instead of in the class, it works fine.

CUnit.h

Code: Select all

class CUnit{
private:
	scene::ISceneManager * smgr;
public:
	scene::IAnimatedMeshSceneNode * node;
	CUnit(IrrlichtDevice * device);
	void remove();
};
CUnit.cpp

Code: Select all

CUnit::CUnit(IrrlichtDevice * device)
{
	smgr=device->getSceneManager();	node=smgr->addAnimatedMeshSceneNode(smgr->getMesh("media/models/FurbolgPanda/furbolgStand.MD3"));
	node->setPosition(vector3df(0,0,0));
	node->setScale(vector3df(0.045f,0.045f,0.045f));
	node->setMaterialFlag(video::EMF_LIGHTING,false);
	node->setAnimationSpeed(10.f);
}
void CUnit::remove()
{

	node->remove();
}
main

Code: Select all

CUnit creepUnit=CUnit(device);
creepUnit.remove();
I'm sure it's some sort of memory or pointer issue but I can't figure out how to fix it. Can anyone tell me what I'm doing wrong?
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Re: remove scenenode in another class

Post by teamAlpha »

Try this :

Code: Select all

if(node)
{
        node->grab();
        node->remove();
        node = NULL;
}
And it should work :) ...
Alpha::AppleBoy
Kazooie
Posts: 13
Joined: Sat Jan 17, 2009 4:26 pm

Post by Kazooie »

Wow, that was so easy, thanks. I didn't even know what grab did :oops: .
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I for myself would do it this way:

Code: Select all

void CUnit::remove(){
   if(node){
      smgr->addToDeletionQueue(node);
      node = 0;
   }
} 
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Re: remove scenenode in another class

Post by vitek »

teamAlpha wrote:

Code: Select all

if(node)
{
        node->grab();
        node->remove();
        node = NULL;
}
You should definitely not need to grab the node. Doing this will actually lead to a memory leak. The if check is something that you should do though. Does your CUnit class have a destructor? If so, I should take this opportunity to remind you about the rule of three.

Travis
Post Reply