Reputation: 23
I have been trying to use std::list, however my work has been sabotaged by following error:
Debug Assertion Failed!
Program: C:\Windows\system32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\list
Line: 289
Expression: list iterators incompatible
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
I have searched a little (a few hours in fact), yet nothing has solved my problem. Error shows itself in following function:
void Plane::createFrame(){
for( int i = 0; i < 400; i++ ){
frame[i] = '_';
}
for (std::list<Unit>::iterator iterator = units_container.begin();
iterator != units_container.end(); ++iterator) {
iterator->draw(frame, 20);
}
}
Where draw function looks like this:
void Unit::draw( char(&world)[400], int width ){
world[ position ] = avatar;
}
Another interesting thing is thing is that I managed to "contain" this error:
void Plane::createFrame(){
for( int i = 0; i < 400; i++ ){
frame[i] = '_';
}
for (std::list<Unit>::iterator iterator = units_container.begin();
iterator != units_container.end(); ++iterator) {
if( iterator->getPosition() < 0 ||
iterator->getPosition() > 399 ){
break;
}
iterator->draw(frame, 20);
}
}
Changed createFrame()
seems immune to previous error.
If I change my code same error my occur in different places (change is necessary, though. Right now it is always createFrame()
that shows error). It always show within almost the same for
maybe with different function being invoked.
createFrame()
is being invoked in a loop. Error usually happens after 100 lap. Changed createFrame()
uses break;
randomly, however error never shows.
There are no other threads that could access list.
I'm using erase()
but error doesn't seem connected in any way.
I hope I didn't miss anything obvious, but now I can't really be sure. Thanks for your time.
Upvotes: 2
Views: 1497
Reputation: 110145
You have an array which is passed to Unit::draw
as char(&world)[400]
, and you are writing into index position
of that array.
If position
is not between 0 and 399, this will result in undefined behavior. This can cause various symptoms: your code may crash, it may work incorrectly, or it may display seemingly unrelated error messages such as the one you are getting about iterators.
When you added the check that position
is between 0 and 399, the error stopped happening here. You say that it now happens elsewhere, so you should check if there is any out-of-bounds array access there (or any other kind of undefined behavior). It may be best to ensure that position
doesn't get assigned an invalid value in the first place.
Upvotes: 1