Search

vector::erase returns incompatible iterator in debug build by BartoszMilewski

Closed
as Fixed Help for as Fixed

5
0
Sign in
to vote
Type: Bug
ID: 545013
Opened: 3/26/2010 2:20:42 PM
Access Restriction: Public
0
Workaround(s)
3
User(s) can reproduce this bug
std::vector<int> v;
v.push_back(1);
v.push_back(2);
auto it = v.erase(v.begin(), v.begin() + 1);
bool b = (it == v.begin());
Assertion "vector iterators incompatible" fails in the last line. This is because the iterator returned by erase has null pointer to the owning container.
Details (expand)

Product Language

English

Version

Visual Studio 2010 Release Candidate

Operating System

Windows 7

Operating System Language

English

Steps to Reproduce

std::vector<int> v;
v.push_back(1);
v.push_back(2);
auto it = v.erase(v.begin(), v.begin() + 1);
bool b = (it == v.begin());

Actual Results

An assertion in debug code

Expected Results

No assertion
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by Roy Fejgin on 6/26/2012 at 5:49 PM
This was fixed in VS2010 SP1. See release notes:
http://support.microsoft.com/kb/983509
Posted by Microsoft on 6/22/2010 at 3:10 PM
Hi,

Thanks again for reporting this bug. We've fixed it, and the fix will be available in VC11.

As always, if you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by Microsoft on 4/5/2010 at 4:28 PM
I suggest synthesizing the return value yourself:

C:\Temp>type purr.cpp
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v;

    v.push_back(11);
    v.push_back(22);
    v.push_back(33);
    v.push_back(44);
    v.push_back(55);

    auto first = v.begin() + 1;
    auto last = v.begin() + 3;

    auto index = first - v.begin();
    v.erase(first, last);
    auto i = v.begin() + index;

    if (i == v.end()) {
        cout << "EPIC FAIL" << endl;
    } else {
        cout << "This should be 44: " << *i << endl;
    }
}

C:\Temp>cl /EHsc /nologo /W4 /MTd purr.cpp
purr.cpp

C:\Temp>purr
This should be 44: 44

Thanks,
STL
Posted by BartoszMilewski on 4/1/2010 at 9:29 PM
Any chance for a workaround?
Posted by Microsoft on 4/1/2010 at 8:21 PM
Hi,

Thanks for reporting this bug. We're keeping it active, and we'll fix it in VC11.

If you have any questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by Microsoft on 3/28/2010 at 10:43 PM
Thanks for your feedback. We were able to reproduce the issue you are seeing. We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 3/27/2010 at 4:10 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.