Page 1 of 1

Deletion of custom scene nodes.

Posted: Tue Nov 24, 2009 1:10 am
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.

Posted: Tue Nov 24, 2009 6:20 am
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

Posted: Tue Nov 24, 2009 3:10 pm
by 3DModelerMan
Oh okay. What do the drop() and remove() functions do then?

Posted: Tue Nov 24, 2009 3:34 pm
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.

Posted: Tue Nov 24, 2009 4:23 pm
by randomMesh
3DModelerMan wrote:Oh okay. What do the drop() and remove() functions do then?
Have a look at the source? :roll:

Posted: Tue Nov 24, 2009 6:08 pm
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

Posted: Wed Nov 25, 2009 2:16 am
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

Posted: Wed Nov 25, 2009 8:50 am
by jeetee
Thx for the clear explanation Vitek.
*Wonders wheter this can be put into the documentation*

Posted: Wed Nov 25, 2009 10:30 am
by hybrid
Already happened 8)