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

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


5
0
Sign in
to vote
ID 545013 Comments
Status Closed Workarounds
Type Bug Repros 3
Opened 3/26/2010 2:20:42 PM
Access Restriction Public

Description

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. 
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 Stephan [MSFT] 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 Stephan [MSFT] 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 Stephan [MSFT] 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)