Sign in to follow this  

Deleting an element of a vector from within itself...

This topic is 3840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had this working until I updated from Visual Studio 6 to 8. Now for some reason it doesn't work anymore and will crash the program as soon as it tries to do it. Basically, I have a vector of the type Object, which is an abstract base class used for every type object in my game. The vector itself represents every object currently loaded. It is a super mario clone and objects such as goombas, turtles, bricks, etc. need to be deleted as they are removed from the game world by the player. To do this before I would do something like this from within a function of the respective class: for(unsigned int i = 0; i < objectVector.size(); i++) { if(objectVector[i] == this) { objectVector.erase(objectVector.begin() + i); delete this; break; } } I run through the entire vector until I find the current object (the one who's member function the code is currently in). I then erase that vector index and delete the object. I am positive that this did work at one point, and now it doesn't

Share this post


Link to post
Share on other sites
Where is it crashing? The erase call? the delete? Somewhere else?

Are you sure the "this" object was allocated with new, and not on the stack?

Share this post


Link to post
Share on other sites
You shouldn't do delete this. You could mark the object as deletable from within itself and check for deletable objects somewhere else and, then, safely delete it.

Share this post


Link to post
Share on other sites
Calling delete on this is incredibly risky, if the compiler has inserted any code after the exit of that loop (say for exception handling) then you could get some strange random crashes. VC6 played fast and loose with the C++ standard and allowed all sorts of crazy code, VC8 is much less forgiving.

Also, do you have a virtual destructor in the object type?

Share this post


Link to post
Share on other sites
If I leave the objectVector.erase() but take out the delete it closes and goes back to the IDE. If I take that out but leave in the delete it will hang and I will have to ctrl-alt-delete the IDE, not the application, the IDE (devenv.exe, VC++ 8)...

Yeah I kind of thought deleting something from inside itself was a bit of a paradox, but it did work... maybe it wasn't working safely before and VC++ 8 is just a lot more strict than 6 was.

No, the Object class does not have a virtual deconstructor, should it?

Share this post


Link to post
Share on other sites
Yes, it should have a virtual destructor. Without it, the code below won't call the subclass' destructor (since a pointer to a superclass won't know it exists):


struct super
{
super() { cout << "c super\n"; }
~super() { cout << "d super\n"; } // should be virtual here
};

struct sub : public super
{
sub() { cout << "c sub\n"; }
~sub() { cout << "d sub\n"; }
};

int main()
{
super *x = new sub;

delete x;

return 0;
}


Output:

c super
c sub
d super

Share this post


Link to post
Share on other sites
Aside from needing a virtual destructor. the code in question looks pretty much okay. I would have written it using iterators, such that the code would not need changing if the container were switched to a list.

Try newing a single object and adding that to objectVector, then calling this function. Does that work?
If so, try adding a few more and call this for one at the beginning middle and end. Any problems yet?
If not, you'll need to post more code.

I believe boost::ptr_container might be useful here, if you'd consider using boost.

Share this post


Link to post
Share on other sites

This topic is 3840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this