Deletion of custom scene 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
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Deletion of custom scene nodes.

Post by 3DModelerMan »

Should memory used for custom scene nodes be deleted in the drop() or remove() functions? Or in the destructor? I have a few things that need to be cleaned up in my custom node on deletion before I can do anything else.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Any resource that is allocated during the lifetime of an object should be cleaned up in that objects destructor. If you need a destructor for cleanup, odds are you should consider adding a copy constructor or assignment operator.

Travis
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Post by 3DModelerMan »

Oh okay. What do the drop() and remove() functions do then?
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
jeetee
Posts: 15
Joined: Tue Oct 13, 2009 3:09 pm
Contact:

Post by jeetee »

From what I understand (still a beginner though) does the drop-method decrement the referencecounter of that object.
The referencecounter keeps track of.. well.. references :roll: to that object. As long as a given element still has a reference, it may lead to null-pointer-errors if destroyed (destructor). However, if no more references are present, you may destroy the object without breaking anything else.

As for remove, I think it just removes the element from the childlist of its parent, nothing else.

[sidenote]
This is out of the top of my head, haven't checked in the code yet, so don't take this for granted.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

3DModelerMan wrote:Oh okay. What do the drop() and remove() functions do then?
Have a look at the source? :roll:
"Whoops..."
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

3DModelerMan wrote:Oh okay. What do the drop() and remove() functions do then?
The documentation isn't really clear on what remove() does, so I'm going to explain it.

drop(): All objects that inherit from IReferenceCounted have a reference count. The count goes up when grab() is called, and it goes down when drop() is called. When the reference count goes to zero on a call to drop() the object deletes itself.

remove(): Scene nodes are organized into a scene graph. Calling remove() will remove the node from the scene graph. All scene nodes are reference counted, and the parent node holds a reference to the node that is being removed. When the node is removed from the scene graph, the parent calls drop() on it. As mentioned previously, if/when the reference count goes to zero, the node deletes itself.

The destructor is called when the scene node is destroyed. This happens when an object allocated on the free store (heap) is deleted, or when it goes out of scope.

Of these functions, only remove() and the destructor are virtual. Non-virtual functions can be overridden, but the override won't be called if the function is invoked through a base class pointer. If you want to do something special when a scene node is removed from the scene graph, you would put it in remove(). If you want to do something special when the object is deallocated, you'd put it in the destructor.

Travis
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Post by 3DModelerMan »

Thanks for the explanation. I always had wondered why there seemed to be two deletion functions for scene nodes. Now I know :D
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
jeetee
Posts: 15
Joined: Tue Oct 13, 2009 3:09 pm
Contact:

Post by jeetee »

Thx for the clear explanation Vitek.
*Wonders wheter this can be put into the documentation*
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Already happened 8)
Post Reply