Pointers deletion at destruction

Discussion about everything. New games, 3d math, development tips...
Post Reply
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Pointers deletion at destruction

Post by Auradrummer »

Hello guys,

Generic C++ question.

I have a small class and I'm programming it's destructor. When I construct it, there are some parameters passed to it, like "IrrlichtDevice * dev". I want to know if when I destroy this class I have to delete the pointer to the device too or it is unecessary.

Thanks.
Professional Software Developer and Amateur Game Designer ;-)
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Re: Pointers deletion at destruction

Post by sudi »

The pointer itself is destroyed by itself but not the object it is referencing to.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Pointers deletion at destruction

Post by hybrid »

If you delete the pointer ('delete myptr' or 'delete [] myarrptr') the underlying object is destroyed. You should never do this with things like the irrlicht device. You should call grab() on those passed in parameters instead, and call drop() in the destructor. This will prevent an intermediate device destruction, while your objects still think that they hold a valid device.
You should only call delete on things that you either created in your constructors (or inside class members, which are then stored in attribute pointers), or on things for which you explicitly take responsibility. This is sometimes very useful, e.g. Irrlicht's CImage can take over the pixel array from the user. That way, the user can create the pixel array and the class is not requested to copy the whole array. But that special constructor or setter explicitly asks the user to give up the memory array, which also needs to be allocated properly (here with new instead of malloc).
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Re: Pointers deletion at destruction

Post by Auradrummer »

Sudi, I think I understood what you said, thanks.

Hybrid, thanks for the "teaching". I work with pointers, but destruction is a bit new for me, now the things are getting clear! Thanks!
Professional Software Developer and Amateur Game Designer ;-)
Post Reply