Public Group

create/delete objects

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

Recommended Posts

In the last few days I tried to understand some basic concepts in making games, and I'm stuck at the object oriented part. I made a few games in a program named game maker in the last years, and I was trying to implement a similar mechanism in c++.

I don't know much c++, maybe the basics. I created a simple ~simulation, where little cells reproduce and die. My problem is, that I create objects, but can't properly delete them. And that results in eating up the memory. If anyone could help me understand what am I doing wrong, I would be grateful. Here's the code:

The creation code:

blbob *tmp;
tmp = new blbob;
tmp->x = x;
tmp->y = y;
blbob_numb++;


in the while loop:

it=obj_list.begin();
while(it!=obj_list.end())  {
if((*it)->z==1)  {
(*it)->step();
it++;
}else  {
it = obj_list.erase(it);
}
}


And if I try to delete it using "delete", i get an error in vc++ and nothing in codeblocks.

in the destructor:

~blbob()  {
it = obj_list.erase(it);
}


in the while loop

while(it!=obj_list.end())  {
if((*it)->z==1)  {
(*it)->step();
it++;
}else  {
delete *it;
}
}


Share on other sites

You need to delete AND erase from your list. Otherwise you will access invalid memory the next time the loop is executed.

Share on other sites

But I'm erasing it in the destructor. And tried erasing it before and after the 'delete' too. I always get the same error.

Share on other sites

What is it? What is obj_list?

Personally I would advise unique_ptr, or possibly shared_ptr+weak_ptr to make objects that reference other objects easier (e.g. "std::weak_ptr<BaseObject> target").

That way you don't have to track down every place things might be removed from the list, the list itself can delete them as needed.

class ObjectTracker
{
public:
typedef std::vector<std::unique_ptr<BaseObject> > Objects;

//no need for an ~ObjectTracker(), the vector will do the right thing

void step()
{
//Not sure what the z is in your code, but assuming some sort of alive/die flag
for (size_t i = 0; i < objects.size(); ++i)
{
bool alive = objects[i]->isAlive();
if (alive)
objects[i]->step();
alive = objects[i]->isAlive();
if (alive)
{
++i; //next object
}
else
{
//erase, delete and next
objects.erase(objects.begin() + i);
}
}
}

//Safe even when called from a BaseObject::step because the loop
//above has no iterators, so the only effect this has in
//increasing the return value of size().
//New objects will have their step method called on the step they
//Another alternative is to have a newObjects list, and copy its
//contents to objects after the step loop.
{
objects.push_back(std::move(object));
}
private:
Objects objects;
};



EDIT:

redesigned it slightly and provided a way to add objects safely even from within step.

Edited by SyncViews

Share on other sites

Does a blbob get put into the list of objects? If this is the case, then the destructor doesn't need to erase from the list, as the portion that iterates through this is responsible for it.

You need to delete AND erase from your list. Otherwise you will access invalid memory the next time the loop is executed.

Like Madhed said, you need to do both at the point of erasing from the list. The destructor should be used to clean-up resources that the object requires and creates (an image, sound etc.), it shouldn't handle erasing itself from a container, as it shouldn't know whether or not it is in a container. It only cares that it is itself and frees up resources correctly, when it goes out of scope/a call to delete is made.

Stitchs.

Share on other sites

Sorry. obj_list is a list. Thanks, I will try your method with a vector.

But am I missing something, because I don't see where you delete the object. Is enough the erase in case of a vector? ->that's what the unique_ptr does?

And thanks for the destructor part Stitchs.

Edited by warcod3

Share on other sites

But am I missing something, because I don't see where you delete the object.

I'm not sure if i got you right. With unique_ptr, you don't need to delete the object, it takes care of that itself.

Share on other sites

Thank you all! :) It's working now.

Share on other sites

Thanks. I will look into it.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 14
• Forum Statistics

• Total Topics
632155
• Total Posts
3004477

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!