I'm having a sporadic problem right now where map::erase() says it worked, but the map sometimes still has the element in it.
auto iterator = device_map.begin(); // std::map<std::string,Device*>
size_t nDeleted = 0;
while (iterator != map.end()) {
Device* device = device_map[iterator->first];
if (device->done()) {
device->close();
cout << "Erasing " << iterator->first << endl;
nDeleted = device_map.erase(iterator->first);
delete device;
}
++iterator;
}
This is called in a std::thread every 500ms. I've tested it by printing the contents of the map before and after the loop, like this:
cout << "device_map = ";
for (std::map<string, Device*>::iterator it = device_map.begin(); it != device_map.end(); ++it) {
cout << it->first << " = " << it->second << "; ";
}
cout << endl;
Sometimes, even if nDeleted == 1, the contents of device_map is the same before and after unmapping the element (except that *device == nullptr because of the delete).
I've also put a breakpoint and device_map.size() is indeed unchanged after calling device_map.erase() does not work.
I have tried this other version of map::erase with the same results:
auto iterator = device_map.begin(); // std::map<std::string,Device*>
size_t nDeleted = 0;
while (iterator != map.end()) {
Device* device = device_map[iterator->first];
if (device->done()) {
device->close();
cout << "Erasing " << iterator->first << endl;
iterator = device_map.erase(iterator);
delete device;
} else {
++iterator;
}
}
Is there something that I'm doing wrong?
Thank you,
Fred