TBH, I'm running out of ideas. But it seems likely that your list structure itself is damaged (e.g. a bad "next" pointer). Even valgrind can't detect all types of memory errors, mainly when still allocated memory is illegally accessed. When everything else fails, it usually leaves careful study of the source code (and many well-placed asserts).
The last few memory-related bugs I can remember were due to uninitialized members, so you might want to look out for that. I can't wait for GCC to support non-static member initialization, which will take care of the problem of uninitialized variables in C++ once and forever.
You also should consistently employ RAII (that means no deletes), which automatically takes care of the vast majority of memory-related problems.
Silly question, but is Bonus being modified in any other thread other than the thread that is executing the code in question. (Meaning, is it possible that bonus was modified, which would then invalidate the iterator to it... that said, the error would be something entirely different but I'm assuming this is multithreaded so I had to ask).
I'm just going to throw some things out there. Is Update const qualified? const_iterator Behaves like const value_type*, it probably would give you a compile warning/error (Seriously doubt this has anything to do with it).
Straight out of Bjarne's book:
Independently of its category, an iterator can allow const or non-const access to the object it points to. You cannot write to an element using an iterator to const - whatever its category. An iterator provides a set of operators, but the type of the element pointed to is the final arbiter of what can be done to that element.
Read and writes copy objects, so element types must have the conventional copy semantics.
As a side note, is it detrimental to check if what the iterator points to is 0 and not call update if it is?
If the vector had to reallocate to make room for more elements I could see this happening. Does Bricks have anything to do with Bonus? Based on the iteration and frequency I'm thinking this is what is happen, now if it is related to the problem, not sure. Try specifying a capacity for the vector and running this test over, see if the same thing happens.
Will do!
I ran across another weird thingy in the mighty land of C++:
When I comment out the entire load function, this happens:
When I run the program with the debugger turned ON: It runs fine.
When I run the program with the debugger turned OFF (but still in debug mode compiled): It crashes.
Oh my oh my! very interesting development: using reserve() worked indeed and the _M_start position is no longer changed. However, when I go into the next function (update()) suddenly these vars show up:
_M_start = 0xfeeefeee
_M_end = 0xfeeefeee
how the hell did that happen?
EDIT: even better: every single one variable of my Level object is bullcrap!
I feel like crying. This is so definitely the most stupid bug ever.
At the start of the load function: amountOfBricks = 0;
I forget to increment this variable each time I add a brick.
In the GameState class, which holds a vector of pairs, the next level is loaded when this variable goes down to 0.
Since I only added one level so far, it tried calling an object never existed. God fucking damn it, why didn't I look there earlier.
And thus the mysterious journey of this bug comes to an end.
I'd like to thank every single one of you for helping me trying to figure it out.