Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:21:06+00:00 2026-06-05T22:21:06+00:00

I need to delete and erase object pointer on a vector. I see this

  • 0

I need to delete and erase object pointer on a vector.
I see this question Should we delete before or after erase for an pointer in the vector? but I can’t erase after delete object pointer.

How to fix it? Here my code.

#include <iostream>
#include <vector>
using namespace std;

class Foo
{
public:
    class bar
    {
    public:
        Foo &_owner;
        int _id;
        bar(Foo &owner,int id) : _id(id), _owner(owner){}
        void remove()
        {
            for(vector<bar*>::iterator it=_owner.vbar.begin();it<_owner.vbar.end();it++)
            {
                if((*it)->_id == _id)
                {
                    //delete object pointer
                    delete * it;

                    //remove element
                    it = _owner.vbar.erase(it); // error on this line.
                }
            }
        }
    };
    vector<bar*> vbar;
    Foo()
    {
        // add 10 elements
        for(int i=0;i<10;i++)
            vbar.push_back(new bar(*this ,i));

        // remove element at 3
        vbar.at(3)->remove();
    }
};

int main(int argc, char *argv[])
{
    Foo foo;
    return 0;
}

[Edited]
I fixed this by delete object pointer from outside Bar class(delete on Foo class)
by adding no longer use bar id to vector on Foo class like this.

#include <iostream>
#include <vector>
using namespace std;

class Foo
{
public:
    class bar
    {
    public:
        Foo &_owner;
        int _id;
        bar(Foo &owner,int id) : _id(id), _owner(owner) {}
        void remove()
        {
            _owner.removeId = _id;
            _owner.removeHelper();
        }
    };
    vector<bar*> vbar;
    int removeId;
    Foo()
    {
        removeId = -1;

        // add 10 elements
        for(int i=0; i<10; i++)
            vbar.push_back(new bar(*this ,i));

        // remove element at 3
        vbar.at(3)->remove();
    }
    void removeHelper()
    {
        for(vector<bar*>::iterator it=vbar.begin(); it<vbar.end(); it++)
        {
            if((*it)->_id == removeId)
            {
                //delete object pointer
                delete * it;

                //remove element
                it = vbar.erase(it);
                break;
            }
        }
        removeId = -1;
    }
};

int main(int argc, char *argv[])
{
    Foo foo;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:21:08+00:00Added an answer on June 5, 2026 at 10:21 pm

    The problem is hidden here

    //delete object pointer
    delete * it;
    

    which in this case is the equivalent of delete this.

    After that, using any part of the current object is undefined behavior. The next round of the loop is not going to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to go through this vector and delete the duplicates. A segmentation fault
I need to delete a file. Occasionally, the file may be locked, in this
I need to delete coma(,) after so many. Say there a string with 4
Hey, I need to delete all images from a string and I just can't
I need to erase elements from a vector based on their value, I've tried
I need to delete only the first appear of a record. This have a
I need to delete all views from my MySQL database. How can I do
I need to delete something with 3 verification checks. how can i do it.
i have 4 models. I want to delete question, but now i cant. Dont
This is a continuation from the previous stackoverflow jQuery question I asked, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.