Deletion of custom scene nodes.
-
- Posts: 1691
- Joined: Sun May 18, 2008 9:42 pm
Deletion of custom scene nodes.
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
My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
-
- Posts: 1691
- Joined: Sun May 18, 2008 9:42 pm
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
My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
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 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.
The referencecounter keeps track of.. well.. references 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.
-
- Posts: 1186
- Joined: Fri Dec 29, 2006 12:04 am
The documentation isn't really clear on what remove() does, so I'm going to explain it.3DModelerMan wrote:Oh okay. What do the drop() and remove() functions do then?
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
-
- Posts: 1691
- Joined: Sun May 18, 2008 9:42 pm
Thanks for the explanation. I always had wondered why there seemed to be two deletion functions for scene nodes. Now I know
That would be illogical captain...
My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar