Answered by:

Trouble with std::map insertion

Question

I have a problem where I call map::insert with apparently good data, and it infrequently (depending on your viewpoint) fails. I was noticing that on occasion, a random sound in my app would not play, and tracked it down by adding the hack code below
and placing a breakpoint when the second "insert" was called.

The data is good, the sound is there... but sometimes the SoundMap.insert just doesn't add a sound (different one each time). As you can see, the hack spins until the insert actually works. What the heck could be causing this?

// This hack added in for now. Tests if the sound above
// can be found in the SoundMap, and loops until it is
// actually inserted.
bool worked = false;
while(!worked)
{
AudioContent* addedContent = GetSoundByName(soundName);
if(addedContent == nullptr)
{
SoundMap.insert(pair<wstring,unique_ptr<AudioContent>>(audioContent->Name, unique_ptr<AudioContent>(audioContent)));
}
else
worked = true;
}