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

  • Home
  • SEARCH
  • 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 8765543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:18:34+00:00 2026-06-13T16:18:34+00:00

Background A while back, I ran into some behaviour that I found very strange

  • 0

Background

A while back, I ran into some behaviour that I found very strange and seemingly incorrect and I filed a bug report with GCC about it. You can see the report and the response I got here:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47305

(I’m going to replicate most of that here.)

At the time, I didn’t understand the answer, but was not a member of StackOverflow and didn’t have anyone to ask about it, so I just hacked a work-around and went on. But recently, I was revisiting this code and I still don’t understand the rationale for this not being a bug, so…

My Question

In the C++ stdlib distribution included with with my Mac (currently OS X, Darwin 12.2.0 x86_64), the implementation of std::vector::erase() from /usr/include/c++/4.2.1/vector.tcc lines 106-116 is shown here:

template<typename _Tp, typename _Alloc>
  typename vector<_Tp, _Alloc>::iterator
  vector<_Tp, _Alloc>::
  erase(iterator __position)
  {
    if (__position + 1 != end())
      std::copy(__position + 1, end(), __position);
    --this->_M_impl._M_finish;
    this->_M_impl.destroy(this->_M_impl._M_finish);
    return __position;
  }

Note that destroy() will be called for the element that is last in the
vector prior to the call to this erase(), instead of being called for the
element pointed to by __position. I believe this is incorrect — I think it
should instead call destroy() for the element pointed to by __position. For
simple POD types, this isn’t that big of a deal, but for classes where the
destructors have side effects (such as smart pointers), it can be critical.

The following code illustrates the problem:

#include <vector>
#include <iostream>

class MyClass
{
    int m_x;
public:
     MyClass(int x) : m_x(x) { }
    ~MyClass()
    {
        std::cerr << "Destroying with m_x=" << m_x << std::endl;
    }
};

int main(void)
{
    std::vector<MyClass> testvect;
    testvect.reserve(8);
    testvect.push_back(MyClass(1));
    testvect.push_back(MyClass(2));
    testvect.push_back(MyClass(3));
    testvect.push_back(MyClass(4));
    testvect.push_back(MyClass(5));

    std::cerr << "ABOUT TO DELETE #3:" << std::endl;

    testvect.erase(testvect.begin() + 2);

    std::cerr << "DONE WITH DELETE." << std::endl;

    return 0;
}

When I compile this with g++ version 4.2.1 (no command line arguments) on my
Mac, it produces the following when I run it:

Destroying with m_x=1
Destroying with m_x=2
Destroying with m_x=3
Destroying with m_x=4
Destroying with m_x=5
ABOUT TO DELETE #3:
Destroying with m_x=5
DONE WITH DELETE.
Destroying with m_x=1
Destroying with m_x=2
Destroying with m_x=4
Destroying with m_x=5

Note that the key line after the “ABOUT TO DELETE #3” message shows that the
destructor was actually called for the (copy of the) fifth thing I added. Importantly, the
destructor for #3 is never called!!

It appears that the version of erase() that takes a range (two iterators) also
has a similar problem.

So my question is, am I wrong to expect that the destructor of the element I am erasing from a vector gets called? It seems that if you can’t count on this, you can’t safely use smart pointers in vectors. Or is this just a bug in the STL vector implementation distributed by Apple? Am I missing something obvious?

  • 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-13T16:18:36+00:00Added an answer on June 13, 2026 at 4:18 pm

    When you erase the element containing a 3, the following elements have to be shifted back to fill the void. Then element #3 gets assigned what #4 has, and #4 gets assigned what #5 has. The last element, #5, is left with whatever value it has since it is about to be deleted anyway.

    When the vector goes out of scope, you see the remaining 4 elements being destroyed.

    If you were to hold smart pointers in your vector, the resources will be properly freed when the assignment operator is called.

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

Sidebar

Related Questions

Some Background I have been using Git for a while now. The projects that
My application downloads and caches photos into coreData in background threads while still allowing
Some background info: I am currently writing a bootloader in protected mode while learning
I have to do a background process which will perform some operations.. while this
So here's the scoop: I wrote a tiny C# app a while back that
I am building an event dispatcher framework that decodes messages and calls back into
First some background: I already have a Result class that I've implemented to carry
We have an application that downloads some files in the background. Our application pops
I ran into an issue that I've been researching for hours to no avail.
I want to fix the Background while only the ListFields scrolls. Current Problem: Scrolling

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.