add regression test for #3215

This commit is contained in:
Niels Lohmann 2021-12-28 23:07:57 +01:00
parent 825d3230d1
commit 21a554f219
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69

View File

@ -227,6 +227,16 @@ inline void from_json(const nlohmann::json& j, FooBar& fb)
j.at("value").get_to(fb.foo.value);
}
/////////////////////////////////////////////////////////////////////
// for #3215
/////////////////////////////////////////////////////////////////////
std::string f_3215()
{
throw std::runtime_error("12");
return {};
}
TEST_CASE("regression tests 2")
{
SECTION("issue #1001 - Fix memory leak during parser callback")
@ -763,6 +773,27 @@ TEST_CASE("regression tests 2")
CHECK(j.dump() == "[1,4]");
}
SECTION("issue #3215 - Exception thrown during initialization causes a memory leak")
{
std::string s;
try
{
// std::map<std::string, std::string> j // no leak
nlohmann::json j // leak
{
{"smth", f_3215()}, // exception thrown here
{"smth", "smth"}
};
s = "initialized";
}
catch (std::runtime_error& e)
{
s = e.what();
}
CHECK(s == "12");
}
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP