Home > OS >  Delete and remove a pointer from a list
Delete and remove a pointer from a list

Time:09-22

I have this code (its a smol version of the code that replicates the error) and it gives a some kind of error with memory. idk just pls help me fix it. It deletes the object so there remains just the nullptr. Idk why but it doesn't want to remove the pointer from the list.

#include <iostream>
#include <list>
// casual include

here i create a class thats the base for all of my other classes

class Object // a virtual class
{
public:
    bool needs_delete = false;

    virtual void tick() {}
    virtual void render() {}
};

a player class that inherits from the Object class i created earlier

class Player : public Object
{
public:
    float x, y; // <-- just look at da code dont read dis

    Player(float x, float y) : // i initialize the "x" & "y" with the x & y the user has set in the constructor
        x(x), y(y)
    {}

    void tick() override // just look at the code
    {
        x  ;
        if (x > 10000)
        {
            needs_delete = true;
        }
    }

    void render() override // just look at the code
    {
        // nothing...
    }
};

just the main function. at this point im just writing text because stackoverflow wont let me post this piece of constant depression. pls help :)

int main()
{
    std::list<Object*>* myObjs = new std::list<Object*>; // a list that will contain the objects

    for (int i = 0; i < 1000; i  ) // i create 1k player just for testing
    {
        myObjs->push_back(new Player(i, 0));
    }

    while (true)
    {
        if (myObjs->size() == 0) // if there are no objects i just break out of the loop
            break;
        
        for (Object* obj : *myObjs) // update the objects
        {
            obj->tick();
            obj->render();

            // some other stuff
        }


        // DA PART I HAVE NO IDEA HOW TO DO
        // pls help cuz i suck

        for (Object* obj : *myObjs) // help pls :)
        {
            // baisicly here i want to delete the object and remove it from the list
            if (obj->needs_delete)
            {
                std::cout << "deleted object\n";
                delete obj;
                myObjs->remove(obj);
            }
        }

    }
}

CodePudding user response:

What about:

myObjs->remove_if([](auto& pObj)
{
    if ( pObj->needs_delete )
    {
        delete pObj;
        return true;
    }
    else 
        return false;
});

CodePudding user response:

You can instead just use a std::vector and loop through your objects with a for loop and store the indexes of the objects which needs to be deleted in another std::vector and remove them. It should be something like this:

std::vector<GameObject*> objects;
std::vector<int> deleteIndex;

for (int i = 0; i < objects.size(); i  ) {
    if (object[i]->delete) deleteIndex.push_back(i);
}

for (auto index : deleteIndex){
    delete objects[i];
    objects.erase(objects.begin()   i);
}
  • Related