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.
Pointers deletion at destruction
-
- Posts: 260
- Joined: Thu Apr 17, 2008 1:38 pm
- Location: Brasopolis - Brazil
Pointers deletion at destruction
Professional Software Developer and Amateur Game Designer
Re: Pointers deletion at destruction
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.
-
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
Re: Pointers deletion at destruction
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).
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).
-
- Posts: 260
- Joined: Thu Apr 17, 2008 1:38 pm
- Location: Brasopolis - Brazil
Re: Pointers deletion at destruction
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!
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