Skip to main content
gamedev.net
🔒 Locked

memory management

Started by daniel_i_l 4 replies 450+ views
Original Post
daniel_i_l
daniel_i_l
I have a stl vector of type CVehicle* and want to put stuff in it like this:

for(int i = 0; i < NumberOfSheep; i++)
     {
      CVehicle* v = new CVehicle(Vector2D(55 -2*i,80));
      Sheep.push_back(v);
     }

If I don't want memory problems do I have to do it like this:

for(int i = 0; i < NumberOfSheep; i++)
     {
      CVehicle* v = new CVehicle(Vector2D(55 -2*i,80));
      Sheep.push_back(v);
      delete v;  //NEW
     }

Or will stl take care of it? Will doing delete wreck the Sheep cause what it was pointing to got deleted? Any explanation here will be appreciated. Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Mike.Popoloski
Mike.Popoloski
Quote:
Will doing delete wreck the Sheep cause what it was pointing to got deleted?

Yes, if you delete it after you give it the pointer, it will probably crash.
You have two choices on what you can do:

1. Go with your vector<CVehicle*>, push a new vehicle on every time you need it, and then at the end of your program, when you are all done, do something like this:
for( UINT i = 0; i < Sheep.size(); i++ ){   delete Sheep.at( i );}


2. Use a vector without the pointers, like this: vector<CVehicle>. That way, when you push the object onto the vector, you can forget about it and not have to worry about cleaning it up. If you do this, you won't have to allocate memory yourself, the STL will handle it for you.
Mike Popoloski | Journal | SlimDX
rip-off
rip-off
You best bet would be to use a smart pointer, becuase deleting v will give you segfaults.

You could also clear the vector just before its goes out of scope:
for( int i = 0 ; i < NumberOfSheep ; ++i ){    delete Sheep;}


But that is messy, and you may forget to do it at some stage.
dave
dave
That second code segment isn't what you want.

First of all, STL vectors store everything by value and dont actually delete anything. You are storing pointers by value in this vector and so when you call say vector.erase(), you are erasing the pointers stored but not what they point to.

To clean this up i would iterate through and manually delete each pointer then call vector.erase().

The alternative is as mentioned above, use boost:shared_ptr. This is a smart pointer and cleans the assigned value up automatically when the shared_ptr object goes out of scope. This is a more advance technique in C++.

I would recommend that you first get it working with the manual memory management and then look to the alternatives once you know what is going on.

Hope that helps,

Dave
Enigma
Enigma
You may also want to consider a Boost.Pointer_Container if the vector is the sole owner of the pointers.

Σnigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.