Do I need to destroy my objects in C++? SOLVED

Started by
5 comments, last by darenking 18 years, 10 months ago
Howdy. My programme, which is a sort of virtual games console, has lots of objects, listed below (you can probably guess what most of them do, more or less). Engine (the game engine) -Joystick -Display -World (controls all of the below...) --Game (stores your score and stuff) --Maze --Actor (lots of instances of this) --Camera (follows the player Actor, one for each layer of the background) I just want to make sure all of my objects are destroyed. I can probably handle the specifics, but just need general advice. I've got a message logging system. In the constructor and destructor for each object, a message is logged to the system saying that the object has been created or destroyed. After running the program the message log reads like this:

Display object exists
Joystick object exists
Game object exists
Maze object exists
Camera object exists
Camera object exists
Camera object exists
World object exists
Engine object exists
Actor object exists
Actor object exists
Actor object exists
Actor object exists
:----------
Engine object destroyed
World object destroyed
Camera object destroyed
Camera object destroyed
Camera object destroyed


As you can see, three of the object types are destroyed at the end, and the others aren't. Obviously you're not going to be able to say why exactly without looking at the program, but perhaps someone could give me some hints? I create the Actors dynamically inside the World object, so that probably explains why the Actors aren't destroyed. I can probably deal with that. My general thoughts: 1. Game and Maze (along with Camera and Actor) are all members of World. Is it possible that Game and Maze are being destroyed as part of World being destroyed? If so, why aren't the destructors called? 2. Even if it is possible that Game and Maze are destroyed as part of World being destroyed somehow without their destructors being called, how come the same isn't true of World? World is a member of Engine, after all, and the World destructer is called. 3. Just in general, can I assume that destructors are *always* called when an object is destroyed? And therefore if they do not log to my system, assuming my system isn't at fault, do I assume that the objects are not being destroyed? Maybe in the destructor for World, for example, I should explicitly tell it to delete all of its member objects, and in the destructor for Engine, I should tell it to destroy all of Engine's member objects (World, Joystick, Display). Hmm! [Edited by - darenking on June 22, 2005 3:19:13 PM]
Advertisement
Are they actual members of the World object or are pointers to them members of the World object? I'm guessing the second since you mentioned dynamic creation and the destructor isn't called. That's a big difference.

Anyway, a rule of thumb: anything you create with new you must destroy with delete when you no longer need it (and similar for new[] and delete[])

Good ways to prevent things like this from happening are putting the objects in a vector<Actor> etc. (or another container) that's a member of the World object or using smart pointers. Note: if you're using inheritance you might want to combine these options: don't create containers of the base class, but containers of smart pointers to the base class.
This is the weird thing, the only one that is stored as a pointer is Actor:

class World{private:	//objects	Maze m_Maze;	std::vector<Actor*> m_Actors;	Camera m_Camera[9];//etc


As you can see, m_Maze is a member object, not a pointer to a member object (or correct me if I'm wrong!). And yet the destructor isn't called!

Is there any reason why a destructor wouldn't be called? Surely you shouldn't have to destroy all your objects. I mean, if I put int x=0 I'm creating an int, and I don't have to destroy that.

I can see why the Actors aren't destroyed: they're created dynamically, pointers to each stored in the vector.

And the Cameras are in an array which may also in a sense be like pointers.

But why isn't m_Maze destroyed? Surely it's just a normal member object?



Any suggestions?
Quote:Original post by darenking
Is there any reason why a destructor wouldn't be called? Surely you shouldn't have to destroy all your objects. I mean, if I put int x=0 I'm creating an int, and I don't have to destroy that.

I can see why the Actors aren't destroyed: they're created dynamically, pointers to each stored in the vector.


standard library containers do proper memory management via there allocator type (all standard library containers are parameterized by allocator type), so they handle there own memory management.

Any resources you acquire yourself is your responsibility to release them back. What that means is if you store pointers as opposed to the actual type in standard library containers its your responsibility to destroy & release any dynamically allocated instances you made that those pointers may refer to.

Unless you have a good specific reason prefer storing the actual type over pointers in standard library containers. If you do have a good reason then you could consider using smart pointers instead, C++ boost library has a package of smart pointers you can use.
The problem I was having with the normal member objects not being destroyed, I think that was a compiling problem, I've just rebuilt the progject and it's now working: every object that is created (aside from those Actors that are created with 'new') now logs a message from its destructor.

As for the Actors...

Someone told me that it's best to store pointers rather than the objects if you plan to put different types of objects in the container, which is the case here, I think.

So basically the container stores pointers and so when the container is destroyed the pointers get destroyed. The objects pointed to by the pointers I have to destroy with delete. That's right isn't it? And I put the delete in the destructor for the object that creates them. The Actors are created in World so I destoy them in the destructor for World.

That sounds reasonable to me. I do exactly the same thing for most of my projects.
Megaboots.

Case closed.

This topic is closed to new replies.

Advertisement