0

I am facing some strange issue in the code below which is compiled in linux,

The below piece of code iterates a map "ObjectsMap" from Begin and inside the iteration loop, I am trying to change the value of a different key in the same map "ObjectsMap".

map<int, ObjectA>::iterator outerItr = reDt->ObjectsMap->begin();
for (; outerItr!= reDt->ObjectsMap->end(); outerItr++)
{
  int parentid = outerItr->second.parentID;
  map<int, ObjectA>::iterator innerItr = reDt->ObjectsMap->find(parentid);
  if (innerItr != reDt->ObjectsMap->end())
  {
     innerItr->second.selfTime = innerItr->second.selfTime + outerItr->second.execTime;
  }
}

While this code executes, I am getting incorrect/unexpected value in the selfTime. But if I add some log inside the if() block then strangely the value is correct. If the log is removed then again I get incorrect value in selfTime.

What is that strange thing here.? Is it because of data race behavior?

Update 1:

selfTime value that I get is not Junk. But it is incorrect value, the value is higher than expected - I mean "outerItr->second.execTime" of some more incorrect keys also gets added to "innerItr->second.selfTime"

Sel_va
  • 588
  • 5
  • 25
  • 1
    what's actual value you get as output for selfTime . I meant is that junk value ?? or other value – Apoorva Raju Jul 11 '19 at 05:13
  • @ApoorvaRaju It is not junk value .. But it is incorrect value, the value is higher than expected - I mean "outerItr->second.execTime" of some more incorrect keys also gets added to "innerItr->second.selfTime" – Sel_va Jul 11 '19 at 05:18
  • Is the code running in multi-thread environment? – Mine Jul 11 '19 at 05:19
  • @Mine This map access is done by a single thread only. – Sel_va Jul 11 '19 at 05:22
  • 1
    This is usually a sign of undefined behaviour somewhere. Try building with sanitizer libraries. – n. m. could be an AI Jul 11 '19 at 05:35
  • 1
    @Sel_va Nothing wrong with the code posted. Often in these cases the error is in the logging code, but you didn't post that. – john Jul 11 '19 at 06:35
  • @john - When there is a file write inside the if() block the error is not getting reproduced. – Sel_va Jul 11 '19 at 06:49
  • 1
    @Sel_va So how are you detecting that the error is being produced? I'm just labouring this point because very often we find that the code in question is correct, and it's the code that is supposed to be showing that there is an error that is bugged. – john Jul 11 '19 at 07:17

0 Answers0