Irrlicht: drop(), remove() and delete
14 Feb 2011Removing objects is important, or you’ll wind up with memory leaks and a shitty game. Irrlicht provides methods to make this easy:
drop() will minus one from the reference counter, and when it reaches 0 the object will be deleted. It’s handy when you reference the object more than once, you’ll call grab() on each reference and drop() when you forget about it. Set the pointer to 0 after droping it for the last time (ie mesh = 0;) so if you reference after deleting by accident, the debugger will pick up on it.
remove() lives in ISceneNode. It will remove this objects pointer from its parent, and call drop(). So don’t call drop() before or after remove(). Again, after remove(), set the pointer to 0.
delete; is just the C++ call. You should never have to use this, as all you objects should be extended from irrlicht objects. If you do have to call it, you’re probably doing something wrong.
The best way to manage memory: is to not manage memory. How? by parenting your objects correctly. Parent your level objects to levelSceneNode, then to change levels you just have to call remove() once, then the children objects will be removed.