Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ map/set iterator not dereferencable

I want to ask you for a hint, as I am a beginner and couldn't find any suitable answer in the internet. I am getting this error: debug assertion failed - map/set iterator not dereferencable at the line that looks like this:

pointA = active->pointNext(timeNext);

with the function pointNext() as I see everything is ok, and what concerns active, I have:

active = setS.data.end();

Some more info:

active is multiset< classA, classB::classC >::const_iterator

setS has: setS.Q, setS.W, setS.T and setS.data, whereby setS.data has inside 0 in square braces. When I have multiset iterator declaration in .cpp file, during debug I cannot enter to see what is inside active, when it is in .h file, I can.

Having in .cpp I cannot enter active, so can imagine it's like pointer(iterator) cannot dereference, because is wrong inside. What if it is empty, i.e. if setS.data is empty? or if there's some trash inside?

I know the thing was running under linux previously, is there some feature which I have to change for running on windows maybe? For example to change a number of template parameters to one only? (to properly ascribe setS.data to active, because I am not sure - do I do it properly?

Sorry for this rather chaotic post, I wanted to add my guesses for someone to neglect them if they are wrong. If something here is unclear or lack of some information, I will gladly add it. Can you please tell me what reasons could cause the dereferencablility error I get and where should I look for it? Because I am stuck and don't know how to proceed.

any help very appreciated, thanks!

like image 814
berndh Avatar asked Nov 21 '12 15:11

berndh


3 Answers

Quite simply, since active points to the container's end(), you are not allowed to dereference it.

I know the thing was running under linux previously

If the code was exactly like this and was "running", all this means that the error has never manifested itself in a manner that you've noticed.

like image 131
NPE Avatar answered Oct 22 '22 12:10

NPE


This is your problem:

active = setS.data.end();

This returns an iterator to one passed the end of the container.
Thus the item that it is pointing at is not valid. You can not call any methods on the object that the iterator is referring too.

If you had done:

active = setS.data.end();
if (setS.data.begin() != active)
{
    // make sure the set is not empty first
    --active;
    active->methodCall(); // This would be OK
}
like image 34
Martin York Avatar answered Oct 22 '22 11:10

Martin York


You cannot de-rederence the iterator returned by a standard library's end() function, as this is "one past the last element". Typically you would iterate over the valid range, i.e. stopping before you reach end():

for(someIteratorType it = setS.data.begin(); it != setS.data.end(); ++it)
{
  it->someMethod();
}

Or, in C++11,

for (const auto& elem : setS.data)
{
  elem.someMethod();
}
like image 22
juanchopanza Avatar answered Oct 22 '22 11:10

juanchopanza