From 21a554f2193246951956f3bf09021e0eb1229106 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 28 Dec 2021 23:07:57 +0100 Subject: [PATCH] :white_check_mark: add regression test for #3215 --- test/src/unit-regression2.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 3ae73ce18..1077a21f5 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -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 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