I created a program, and it uses the vector.h #include, and iterators, etc… But when I run the program, under certain circumstances (I’m still trying to figure out what those would be) I get an assertion error refering me to line 98 of vector.h. I went to line 98 of vector.h and got this:
#if _HAS_ITERATOR_DEBUGGING
if (this->_Mycont == 0
|| _Myptr < ((_Myvec *)this->_Mycont)->_Myfirst
|| ((_Myvec *)this->_Mycont)->_Mylast <= _Myptr)
{
_DEBUG_ERROR("vector iterator not dereferencable");
_SCL_SECURE_OUT_OF_RANGE;
}
Can somebody please tell me what this means and what in my program is causing this assertion?
NB: Line 98, for the record, is the one that begins “_DEBUG_ERROR(“vect…”
NB: This is the code in my program that I BELIEVE triggered the error, I’m not entirely sure, though.
CODE:
for(aI = antiviral_data.begin(); aI < antiviral_data.end();)
{
for(vI = viral_data.begin(); vI < viral_data.end();)
{
if((*aI)->x == (*vI)->x && (*aI)->y == (*vI)->y)
{
vI = viral_data.erase(vI);
aI = antiviral_data.erase(aI);
}
else
{
vI++;
}
}
if((*aI)->x >= maxx || (*aI)->x < 0 || (*aI)->y >= maxy || (*aI)->y < 0)
{
aI = antiviral_data.erase(aI);
}
else
{
aI++;
}
}
The runtime is detecting that you are dereferencing an iterator that is before begin() or after end().
Imagine if you delete the last item in the
antiviral_datavector in line 7:aIgets set toantiviral_data.end(), and when you dereference it in line 14:and also in line 5:
You are dereferencing an out of bounds iterator.
The fix is to check that
aI != antiviral_data.end()after the erase call to make sure you haven’t hit the end of the vector before you continue on using it.