• 主页
  • 在使用GCC的unordered_map :: insert KeyEqual异常期间发生内存泄漏-破坏了强有力的异常安全保证吗?

在使用GCC的unordered_map :: insert KeyEqual异常期间发生内存泄漏-破坏了强有力的异常安全保证吗?

我正在使用GCC 7.3.1,但也在coliru(我相信它是9.2.0版)上进行了测试。使用以下内容进行构建:

g++ -fsanitize=address -fno-omit-frame-pointer rai.cpp

这里是rai.cpp

#include <iostream>
#include <unordered_map>

int main()
{
    try
    {
        struct MyComp {
            bool operator()(const std::string&, const std::string&) const {
                throw std::runtime_error("Nonono");
            }
        };

        std::unordered_map<std::string, std::string, std::hash<std::string>, MyComp> mymap;
        mymap.insert(std::make_pair("Hello", "There"));
        mymap.insert(std::make_pair("Hello", "There")); // Hash match forces compare
    } catch (const std::exception& e) {
        std::cerr << "Caught exception: " << e.what() << "\n";
    }
}

运行它会导致:

> ./a.out
Caught exception: Nonono

=================================================================
==72432==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:
...

Direct leak of 4 byte(s) in 1 object(s) allocated from:
...

Indirect leak of 60 byte(s) in 2 object(s) allocated from:
...

SUMMARY: AddressSanitizer: 96 byte(s) leaked in 4 allocation(s).

我看不到Visual C ++有任何内存泄漏(Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x64)。

这是否会破坏unordered_map::insert(https://stackoverflow.com/a/11699271/1958315)的强大异常安全保证?这是GCC STL中的错误吗?

转载请注明出处:http://www.jxbyjx.net/article/20230511/888962.html