Problems removing ISceneNode from within a custom 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
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Problems removing ISceneNode from within a custom class

Post by shadowslair »

Hi there! After about a year I decided to continue my old project, but seemingly I have forgotten my basic C++ knowledge (actually never been a good programmer at all :oops: ),so I have some problems removing an "ISceneNode" and my "CMyCustomClass" node.

Code: Select all

//======================================================================
//Main.cpp
//======================================================================
CMngr = new CreationManager();


scene::ISceneNode*  myNode = Smgr->addCubeSceneNode(1);
 CMyCustomClass* myCustomNode = CMngr->createCustNode(myNode);

//======================================================================
//CMyCustomClass.h
//======================================================================
class CMyCustomClass 
{
public:
	CMyCustomClass(scene::ISceneNode* node);
	~CMyCustomClass();
	void update();

private:
	scene::ISceneNode* irr_Node;

};

//======================================================================
//CreationManager.cpp
//======================================================================
CMyCustomClass* CreationManager::  createCustNode(scene::ISceneNode* node)
{

        CMyCustomClass* cn = new CMyCustomClass(node);
	
	return cn;
}
//=====================================================================================================
What I need to do is to remove/delete the CMyCustomClass* myCustomNode (holding the ISceneNode as "irr_Node") as a whole or at least the ISceneNode* myNode only (both created in the beginning)
which should be done either from within the "update();" from the CMyCustomClass ot from the CreationManager class- doesn`t really matter.


Should I create a function (in my CMyCustomClass) like: "removeCustNode();" and later call "myCustomNode->removeCustNode();" and what will it contain?
I spend some time searching the forum and found that code:

Code: Select all

if (node)
		{
		//Smgr->addToDeletionQueue(node); 
      //node->drop();
		node->remove();
		node = NULL;
		}
I tried calling all these functions from different locations, but the game crashes every time I try removing the node- so honestly I have no idea how to remove/delete those nodes... :? Please tell me what you think about that.

Thanks in advance. :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
radical-dev
Posts: 45
Joined: Thu Apr 24, 2008 7:54 pm
Location: Wickede, Germany

Post by radical-dev »

Hi!

tried to call remove() BEFORE drop() ? If you drop an element, the pointer to your node leads to a crash.
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

radical-dev wrote:Hi!

tried to call remove() BEFORE drop() ? If you drop an element, the pointer to your node leads to a crash.
Hi, radical-dev. No, I didn`t call it before any of these calls. They are willingly commented- I just wanted to show that I`m aware of the functions and tried all of them, and what I need to do is to delete my whole custom node, which holds the ISceneNode, and the game crashes when I do so. So, the problem is about deleting such a thing in C++ at all, not specially in Irrlicht, though calling ->remove(); the irrlicht node crashes the game. I need to delete both the pointers actually. :roll:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

presumably it's a typo in the thread that you're deleting node instead of irr_Node?
Image Image Image
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

JP wrote:presumably it's a typo in the thread that you're deleting node instead of irr_Node?
I cannot understand what do you mean. Sorry, but my programming english is kinda poor. All I need to do is to remove/delete the nodes I mentioned. Like calling "delete ClassNode", but without the crash, caused by the ISceneNode pointer. :roll:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

In the code below you're deleting a variable called 'node':

Code: Select all

if (node) {
      //Smgr->addToDeletionQueue(node);
      //node->drop();
      node->remove();
      node = NULL;
}
But in your class declaration you only have a node called 'irr_Node':

Code: Select all

class CMyCustomClass
{
public:
   CMyCustomClass(scene::ISceneNode* node);
   ~CMyCustomClass();
   void update();

private:
   scene::ISceneNode* irr_Node;
}; 
So where has this 'node' variable appeared from?
Image Image Image
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Normally you would do it in destructor:

Code: Select all

~CMyCustomClass()
{
   if(irr_Node) irr_Node->remove();
}
This will remove irr_Node when your custom node gets deleted.

However I see that you do not create irrlicht node inside your class but you pass it in to your custom node from outside in constructor:

Code: Select all

CMyCustomClass(scene::ISceneNode* node);
If some other part of your code (outside of your custom class) holds pointer and will attempt to use it after your custom node is deleted (and irrlicht node along with it) then your program will crash.

[EDIT:] I suppose that irr_Node and node are the same. Means you do somewhere in your constructor:

Code: Select all

CMyCustomClass(scene::ISceneNode* node)
{
   irr_Node = node;
}
Your code is not specific about this.

If you are using node outside your custom class then you do this instead:

Code: Select all

CMyCustomClass(scene::ISceneNode* node)
{
   irr_Node = node;
   irr_Node->grab();
}

~CMyCustomClass()
{
   irr_Node->drop();
}
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

It worked nicely!!! I`m simply calling ->remove(); from the deconstructor, and after a call form the CreationManager "delete MyCustomNode;" the IsceneNode is removed. It turned that the problem was beacuse of calling the empty node pointer from another function, which needed a bit of tweaking, but as a whole I think everything is fine now (and for now). :D

Once again THAKS A TON EVERYBODY!!! For your time and your efforts helping me. :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Post Reply