irrlicht memory management

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
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

irrlicht memory management

Post by fargoth »

Hi, I was wondering - If I call smgr->clear() like so:

Code: Select all

 
ISceneManager smgr = ..
//build the scene graph, have fun, keep referenced to important scene nodes, etc.
//decide to reset scene graph (change level)
smgr->clear();
//build new scene graph and have more fun
 
Do I have to also drop the references to the old scene graph and reset them to nullptr, or does that happen automatically?
If I don't drop my references and just reassign them to new scene nodes from the newly created level, do I have a memory leak?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: irrlicht memory management

Post by CuteAlien »

Irrlicht uses reference-counting. If you want to take part in that you have to do it explicitely. So you if you have a node and say myNode->grab() - then you increase the reference count and the node will stay in memory even after a clear() until you call myNode->drop(). If you don't do that then the node will be removed, but you then have to be careful to not use your pointer anymore after a clear() as it will now point to invalid memory.
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
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

Re: irrlicht memory management

Post by fargoth »

So if I don't use grab, I don't need to use drop after the clear() function?
If I understand correctly, functions which have create as part of their name increase the ref count - are they deleted after clear() provided i did not call grab, or do I have to make sure I drop them after I'm done with them?

And what about something like getMesh? do I have to grab it for it to remain after clear()?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: irrlicht memory management

Post by CuteAlien »

Functions which start with create act the same way as if you call grab() once. Meaning their reference-counter starts with 1 instead of 0. Aside from that - it's simply a counter. The objects are deleted when the counter is zero. Irrlicht increases and decreases the counter for it's own classes (like SceneManager) and leaves it up to you if you want to use the Irrlicht mechanism as well or not.

Meshes and Textures have the same mechanism. But they are additionally cached in the MeshCache and TextureCache which grabs an additional reference. They stay in there unless you explicitely remove them from the caches.
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
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

Re: irrlicht memory management

Post by fargoth »

I understand it's just a counter.. I just want to make sure I got it correctly:

If I use a create function to get an animator node, add it to another node, and than call clear on scene manager - I would still have that animator node because it had 2 refs, and now it has one? So I have to drop the animator or it would leak?

Just making sure of when to use the RAII wrapper...

And thanks for the patient replies :D
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: irrlicht memory management

Post by CuteAlien »

Yes, you got that correct. When you have a pointer you got from a function that starts with 'create' then you always have to call drop() at some point or you get a memory leak.
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
Post Reply