简体   繁体   中英

“map/set iterators incompatible” while calling std::map::find

C++ 0x11 (sort of. You know, Microsoft, and all...), Visual Studio 2010 SP1, Windows 7 Enterprise.

I've got a function:

// typedef boost::variant</*stuff*/> value_t;
// typedef unsigned short key_t
// typedef std::map<key_t, value_t> map_t

value_t find(const key_t key, const map_t &map)
{
    const map_t::const_iterator iter(map.find(key));

    // ...More stuff and then return.
}

Within the body of std::map::find, there is an iterator comparison, and I'm getting the error message "map/set iterators incompatible", as defined in VC\\include\\xtree:321. The nature of this error is that iterators from two different containers are being compared to one another, but the error is coming from within a containers own find function!!! Retorically, where could another container's iterator be coming from.

Ok, symptoms:

  • This is a threaded application.
  • It only happens at runtime, during a randomized stress test of the actual application.
  • We're not seeing any sort of invalid or obvious garbage fields.

My questions to the greater community:

Supposing this is a timing error in a threaded environment, can a change to the internal state of the map, mid way through this process, modify what would be the value of template class _Tree_const_iterator::_Getcont()? I'm supposing the map is being cleared and repopulated in another thread before the find in this thread completes. Could that alone do it? Or how about a swap or move operation?

I'll track down the missing mutex lock or whatever, I just want to make sure I understand all the plausible means I can enter this state, given this context.

Supposing this is a timing error in a threaded environment

Then all bets are off. If your code is threaded and isn't thread safe, then you can get all kinds of errors. std::map is not thread-safe (you can't access it while it's being modified), so you could get plenty of kinds of errors.

Make sure your code is thread safe before assuming something else is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM