noob question about irrArray

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
kendric
Posts: 71
Joined: Tue May 29, 2007 9:05 pm

noob question about irrArray

Post by kendric »

Looking at the source code it appears when you call clear on an array it calls destructors of everything in the array.
Does this mean if i make an array of * and assign them things with new, that I don't have to delete them myself when im done with the array?

Thanks, my java background is making me question this :)

Also, i have a map of fonts that i created with the new command using the truetype library. I want to drop them before cleaning up. If they are auto destroyed when I remove it i can't drop them, if I drop them first then remove them, won't that mess up the map since drop deletes the value and then what happens when the array destructs and auto deletes again.

If anyone has any answers to these questions on whats the proper way to go about this I would appreciate it :)

Thanks.
CuteAlien
Admin
Posts: 9694
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: noob question about irrArray

Post by CuteAlien »

kendric wrote:Looking at the source code it appears when you call clear on an array it calls destructors of everything in the array.
Does this mean if i make an array of * and assign them things with new, that I don't have to delete them myself when im done with the array?
No, I guess it has to call the destructors explizitely because of it's way of memory handling. But it won't (and shouldn't) delete memory which was allocated by yourself. So delete your pointers before clearing the array.
kendric wrote: Thanks, my java background is making me question this :)
That's one of the big differences ;-)
kendric wrote: Also, i have a map of fonts that i created with the new command using the truetype library. I want to drop them before cleaning up. If they are auto destroyed when I remove it i can't drop them, if I drop them first then remove them, won't that mess up the map since drop deletes the value and then what happens when the array destructs and auto deletes again.
Although I avoid using the irrlicht map, I suppose it works the same way like the array. So you can/should delete the objects first and then delete your map.
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
kendric
Posts: 71
Joined: Tue May 29, 2007 9:05 pm

Post by kendric »

Ok but a follow up, you say delete the pointer first. That will result in a destructor call right. Won't that happen again when they call the destructor? 2 destructor calls sounds bad.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The destructors are called on the elements of the array. If you have an array of pointers, the destuctors are called on the pointers, not on the objects they point to. This allows to put a list of pointers even into a temporary array without loosing the elements when going out of scope. A destruction of a pointer is simply a no-op, so no two destructor calls.
Also note that you often drop an object in Irrlicht, instead of destructing it. But you have to do both things on your own.
kendric
Posts: 71
Joined: Tue May 29, 2007 9:05 pm

Post by kendric »

AHA thats the piece i was missing. Thanks a bunch guys!
Post Reply