Correct new/delete? for nodes?

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
Mars_999
Posts: 136
Joined: Sat Dec 08, 2012 7:59 pm

Correct new/delete? for nodes?

Post by Mars_999 »

I am trying to get this cleared up before I get to much code done...

From what I have read if you use create() you call drop() or use grab() you call drop()....

Now if I have this code

Code: Select all

 
//NX::ShapeSceneNode* shape; derived from irr::scene::ISceneNode
 
NX::ShapeSceneNode* shape = new NX::ShapeSceneNode(p->GetDevice()->getSceneManager()->getRootSceneNode(), 
                                   p->GetDevice()->getSceneManager(), -1, 
                                   p->GetDriver()->getTexture(textureFilename),
                                   sides, false);
 
do I need to call delete on shape ever? I am doing this for now...

Code: Select all

 
inline void KillShape()
    {
        if(shape)
        {
            shape->removeAll();
            shape->remove();
                        //delete shape; //should I add this again?
            //shape = nullptr;
        }
    }
 
Marthog
Posts: 31
Joined: Sun Oct 03, 2010 8:33 pm
Contact:

Re: Correct new/delete? for nodes?

Post by Marthog »

No, you shouldn't use delete.
Just call remove, which automatically removes it from it's parent's list and calls drop. If another grab exists, the scenenode won't be deleted, so no access to an invalid object access will happen, but if no other drop exists, it will be deleted.
But after a creation with new, the scenenode has an reference count of 1 and it's parent calls grab, so you have to drop it after the creation.

You don't need to call removeAll, because all children are removed if the deletion of the node.
Rust fanboy
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Correct new/delete? for nodes?

Post by CuteAlien »

That's not correct. In Irrlicht you should always call drop() when you use new. While calling remove() will remove nodes from a scenemanager and drop the scenemanager reference - it certainly will not(!) drop references you created yourself (meaning using 'new' or a function starting with create). So in Irrlicht would call node->remove() and then node->drop() when you created it with new. Or if you don't need the reference yourself you can even call drop() directly after adding it to the scenemanger - then you only need to call remove() later.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Correct new/delete? for nodes?

Post by kklouzal »

Just to make sure I and everyone else understands correctly, if you do something like the following:

Code: Select all

irr::scene::IDynamicMeshBuffer *mb = new irr::scene::CDynamicMeshBuffer(irr::video::EVT_STANDARD, irr::video::EIT_16BIT);
When you're done with mb you should first call

Code: Select all

mb->drop();
then:

Code: Select all

delete[] mb;
?
Dream Big Or Go Home.
Help Me Help You.
Mars_999
Posts: 136
Joined: Sat Dec 08, 2012 7:59 pm

Re: Correct new/delete? for nodes?

Post by Mars_999 »

I am not sure on the delete either, but you wouldn't do delete[]

So far from what CuteAlien says I have this...

Code: Select all

 
//class ShapeSceneNode : public irr::scene::ISceneNode; just to show where this is inherited from
ShapeSceneNode* shape = new ShapeSceneNode(p->GetDevice()->getSceneManager()->getRootSceneNode(), 
                                                                       p->GetDevice()->getSceneManager(), -1, 
                                                                       p->GetDriver()->getTexture(textureFilename),
                                                                       sides, false);
if(shape)
{
shape->removeAll();
shape->remove();
shape->drop();
shape = nullptr;
}
 
now do we do the above or this

Code: Select all

 
if(shape)
{
shape ->remove();
shape ->drop();
delete shape ;
shape =nullptr;
}
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Correct new/delete? for nodes?

Post by CuteAlien »

@kklouzal: No, you shouldn't do the delete - and especially not the delete[] (you only ever do that when you are creating arrays of a type - not when you create a single CDynamicMeshBuffer object - it does not matter if that object internally uses arrays or something. You never should need to know about what's inside a class. You only care about the objects you create - which is in this case just one instance of CDynamicMeshBuffer)

@Mars_999: You don't need the removeAll - children are removed automatically anyway on remove (removeAll is when you want to remove the children but not the node itself). Your second solution with the additinal delete should usually crash - if it doesn't it might mean you have one more reference somewhere to this object so you might want to check this (you can call getReferenceCount () for debugging or use directly watches in the debugger to check the reference-counter in different places).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Mars_999
Posts: 136
Joined: Sat Dec 08, 2012 7:59 pm

Re: Correct new/delete? for nodes?

Post by Mars_999 »

Thanks CuteAlien for the clarification!!!
Post Reply