Skip to content

bytell_hash_map: Duplicate item found while traversing map after calling erase() #41

@PaoloBaerlocher

Description

@PaoloBaerlocher

Hello, while using bytell_hash_map I came across this issue: after erasing an item while traversing the map, I get the same item two times. Here is a small repro case:


    typedef ska::bytell_hash_map<const void*, int> StaticObjectsToUpdate;
    //typedef std::unordered_map<const void*, int> StaticObjectsToUpdate;
    StaticObjectsToUpdate testMap;
    int iteration = 0;
    while (1)
    {
        iteration++;
        // Insert some items in the map
        for (int i = 0; i < 10; i++)
        {
            void *ptr = malloc(4);
            testMap[ptr] = 3;
        }
        // Process
        std::vector<const void *> tmpList;
        for (auto it = testMap.begin(); it != testMap.end();)
        {
            tmpList.push_back(it->first);
            auto& nbFrames = it->second;
            if (--nbFrames == 0)
            {
                it = testMap.erase(it);
            }
            else
            {
                it++;
            }
        }

        // Test consistency : look for duplicates
        for (int i = 0; i < tmpList.size(); i++)
        {
            for (int j = i + 1; j < tmpList.size(); j++)
            {
                if (tmpList[i] == tmpList[j])
                {
                    printf("iteration %d: same items found : %x.\n", iteration, tmpList[i]);  // Should not happen
                    exit(0);
                }
            }
        }
    }

The issue does not happen when using flat_hash_map or unordered_map .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions