I'm using GCC 7.3.1, but also tested on coliru, which I believe is version 9.2.0. Build with the following:
g++ -fsanitize=address -fno-omit-frame-pointer rai.cpp
Here's 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";
}
}
Running it results in:
> ./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).
I don't see any memory leaks with Visual C++ (Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x64
).
Does this break the strong exception safety guarantee of unordered_map::insert
(https://mcmap.net/q/493408/-where-can-i-find-all-the-exception-guarantees-for-the-standard-containers-and-algorithms)? Is this a bug in the GCC STL?
std::unordered_map::insert
clearly says "1-4) If an exception is thrown by any operation, the insertion has no effect." (emphasis is mine) from here en.cppreference.com/w/cpp/container/unordered_map/insert – Tammeratammi