我使用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).我没有看到VisualC++ (Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x64)有任何内存泄漏。
这是否破坏了unordered_map::insert (https://stackoverflow.com/a/11699271/1958315)的强异常安全保障?这是GCC STL的错误吗?
发布于 2019-12-12 15:05:07
标准规定的担保(引用最新草案):
container.requirements.general
除非另有规定(请参阅associative.reqmts.except、unord.req.except、deque.modifiers和vector.modifiers),否则在本条款中定义的所有容器类型都满足下列附加要求:
associative.reqmts.except
对于关联容器,如果从插入或插入函数中插入单个元素的任何操作引发异常,则插入无效。
unord.req.except
对于无序关联容器,如果在插入或插入单个元素的函数中由容器的散列函数以外的任何操作引发异常,则插入无效。
据我所知,“没有效果”意味着“没有内存泄漏”。在这样的解释下,我会认为漏洞是一种缺陷。
https://stackoverflow.com/questions/59306779
复制相似问题