1

I'm seeing some unusual behavior when I compare two iterators.

vector<list<MyClass*>>   vlWatchers(10);
list<MyClass*>::iterator itCurrent, itEnd;

for (int i(0); i <= 9; ++i)
{
    itCurrent = vlWatchers[i].begin();
    itEnd = vlWatchers[i].end();
    while (itCurrent != itEnd)
    {
        //code
    }
}

will cause a liste iterators incompatible error on the while() line, and appears to happen when i = 0, although only some of the time.

Upon further investigation after the error is called, itEnd and itCurrent are both equal to 0xcdcdcdcd. The weird part is when i step into the != compare operator, the "this" pointer BECOMES 0xcdcdcdcd. Shouldn't 0xcdcdcdcd be the value that's stored in the iterators, not the address of the iterators themselves? or is there some sort of iterator black magic where the iterator both stores a value and IS the value? This is part of a larger project, but the error is repeatable.

Thank you in advance for any help!

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
Paul
  • 11
  • 1
  • 1
    Are you using a C++0x-compiler? If not, your example has a syntax error in the first line (the double closing brackets of the template). – Björn Pollex Jun 30 '11 at 17:57
  • Also, can you state which compiler you are using? – Björn Pollex Jun 30 '11 at 18:01
  • @Space_C0wb0y: The latest MSVC enables (Microsoft's not-very-complete implementation of) C++0x by default. – kennytm Jun 30 '11 at 18:04
  • 0xcdcdcdcd means ["uninitialized heap memory"](http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations), by the way. – kennytm Jun 30 '11 at 18:06
  • @Paul Are you erasing or deleting the content of the list using iterators? – karlphillip Jun 30 '11 at 18:08
  • @karlphillip quite possibly, this is being used for widgets, and there are way too many going on in the current project. The solution we had was to make the currentIterator a member variable and increment it on a remove if the what the current iterator was looking at was requested to be removed. – Paul Jun 30 '11 at 18:10
  • @Space_C0wb0y in the actual code it typedefs list, but i haven't seen any compiler errors about syntax – Paul Jun 30 '11 at 18:11
  • 2
    I suspect that you might be invalidating iterators because of erase/delete operations. Check this thread to make things safe: http://stackoverflow.com/questions/4645705/vector-erase-iterator/4645727#4645727 – karlphillip Jun 30 '11 at 18:13
  • 1
    @Marius: That was just MSVC++ not conforming to the standard. Until C++0x, that is not valid, though MSVC++ will accept it. – Billy ONeal Jun 30 '11 at 18:31
  • @Marius Bancila: what is with other platforms??? Where does Paul tell anything abount MSVC VC++ and more?? Maybe he is on a unix platform, or just uses plain c++, using another compiler, what ever? OK, now he told its VS2005 =) – Thomas Berger Jun 30 '11 at 18:33
  • Visual Studio 2005 is the compiler – Paul Jun 30 '11 at 18:33
  • @billy, @thomas, I read between the lines... I thought it was about VC++ – Marius Bancila Jun 30 '11 at 18:34
  • aha. so it was VC++ after all... I removed the comment though – Marius Bancila Jun 30 '11 at 18:36
  • Are you trying to debug a **Release** mode executable? If so, you'll see lots of crazy stuff in the debugger that doesn't make sense. If that's the case, instead try debugging with the **Debug** build. – Adam Rosenfield Jun 30 '11 at 20:05

1 Answers1

0

Let's follow the logic chain (assuming the first few lines of your for loop are exactly as described):

  • itCurrent->this == 0xcdcdcdcd, therefore ...
  • itCurrent == 0xcdcdcdcd, therefore ...
  • vlWatchers[i].begin() returned 0xcdcdcdcd, therefore ...
  • vlWatchers[i] is invalid, therefore perhaps ...
    • i >= vlWatches.size(), or
    • vlWatchers is invalid

I vote for vlWatchers.size() == 0 somehow. Can you add a check to your method to detect that case?

Robᵩ
  • 163,533
  • 20
  • 239
  • 308