Page 1 of 1

noob question about irrArray

Posted: Tue Aug 07, 2007 9:18 pm
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.

Re: noob question about irrArray

Posted: Wed Aug 08, 2007 4:33 pm
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.

Posted: Wed Aug 08, 2007 5:10 pm
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.

Posted: Wed Aug 08, 2007 5:18 pm
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.

Posted: Wed Aug 08, 2007 5:47 pm
by kendric
AHA thats the piece i was missing. Thanks a bunch guys!