lundi 21 juin 2021

Crash during erase from map

I have a crash as consequence of unexpected behaviour.. I want to ask how could I modify the code to defence against this crash what happened.

   void SubscManag::handleNotif(Notif notif)
    {
   
        std::cout << "Current subscribe size: " << subs_.size();
        std::map<int, SubscData>::iterator it;
        {
            std::lock_guard<std::recursive_mutex> lock(mutex_);
            it = subs_.begin();
        }
        for (;;)
        {
            std::lock_guard<std::recursive_mutex> lock(mutex_);
            if (it == subs_.end())
            {
                break;
            }
            if (it->second.hasToBeErased)
            {
                std::cout << "Erase id : " << it->first;
                it = subs_.erase(it);
                continue;
            }
            if (cond)
            {
                // ....
                ++it;
                continue;
            }
            // ....
            ++it;
        }
    }

Crash is at line it = subs_.erase(it); with this

 Erase id : 2
 Erase id : 3
 Erase id : 28473456

So probably Element with id : 28473456 does not exist or was corrupted but how could I defense against crash ? Thank you.

Aucun commentaire:

Enregistrer un commentaire