lundi 5 octobre 2020

Why the erase method of std::unordered_set is crashing? [closed]

Given the following class:

//MyClass.h
class MyClass
{
public:
    //... regular ctor, dtor, etc...
private:
    std::unordered_set<std::wstring> failedItems;
    std::mutex mutexFailedItems;
    void eraseFailedItem(std::wstring path);
};

//MyClass.cpp
//the following method is the only caller 
bool MyClass::performAction(std::string &&stringFromStream)
{
    Action action;
    stw::wstring path;
    bool failedToProcess = getCurrentItem(action, path);

    while (true)
    {
        switch (action)
        {
            //...other cases
            case Item::Action::Read:
            {
                std::ifstream file(path, std::ios::binary, _SH_DENYWR);
                if (file.good())
                {
                    eraseFailedItem(path);
                    file.seekg(std::ios::end);
                    shared->reserve(size_t(file.tellg()));
                    file.seekg(std::ios::beg);
                    shared->assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
                    return true;
                }
                else
                {
                    insertFailedItem(path.getPathEscaped());
                    break;
                }
            }
        }
    }

    return false;
}

void MyClass::eraseFailedItem(std::wstring path)
{
    const std::lock_guard<std::mutex> lock(mutexFailedItems);

//    if I don't surround the erase method with a if (!failedItems.empty())
//    it crashes 100% of the time when in RELEASE mode
//    failedItems.erase(path);

    if (!failedItems.empty())
    {
        failedItems.erase(path);
    }
}

When I call eraseFailedItem for 5 or more times

Then the app crashes with an access violation exception

NOTES:

  1. The method insertFailedItem is not even being called - so the shared mutex so far shouldn't be the problem.
  2. Those are the only places where failedItems is used. No one else calls it, not even private methods of the class.

Aucun commentaire:

Enregistrer un commentaire