Skip to content

Commit

Permalink
nlohmann#394 fixed memory leak in unit-allocator, found by clang's fs…
Browse files Browse the repository at this point in the history
…anitize
  • Loading branch information
Daniel599 committed Dec 25, 2016
1 parent ceed02a commit 3f94786
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions test/src/unit-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ struct my_allocator : std::allocator<T>
}
};

// allows deletion of raw pointer, usually hold by json_value
template<class T>
void my_allocator_clean_up(T* p)
{
assert(p != nullptr);
my_allocator<T> alloc;
alloc.destroy(p);
alloc.deallocate(p, 1);
}

TEST_CASE("controlled bad_alloc")
{
// create JSON type using the throwing allocator
Expand All @@ -131,7 +141,8 @@ TEST_CASE("controlled bad_alloc")
{
next_construct_fails = false;
auto t = my_json::value_t::object;
CHECK_NOTHROW(my_json::json_value j(t));
auto clean_up = [](my_json::json_value& j){ my_allocator_clean_up(j.object); };
CHECK_NOTHROW(my_json::json_value j(t); clean_up(j));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value j(t), std::bad_alloc);
next_construct_fails = false;
Expand All @@ -140,7 +151,8 @@ TEST_CASE("controlled bad_alloc")
{
next_construct_fails = false;
auto t = my_json::value_t::array;
CHECK_NOTHROW(my_json::json_value j(t));
auto clean_up = [](my_json::json_value& j){ my_allocator_clean_up(j.array); };
CHECK_NOTHROW(my_json::json_value j(t); clean_up(j));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value j(t), std::bad_alloc);
next_construct_fails = false;
Expand All @@ -149,7 +161,8 @@ TEST_CASE("controlled bad_alloc")
{
next_construct_fails = false;
auto t = my_json::value_t::string;
CHECK_NOTHROW(my_json::json_value j(t));
auto clean_up = [](my_json::json_value& j){ my_allocator_clean_up(j.string); };
CHECK_NOTHROW(my_json::json_value j(t); clean_up(j));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value j(t), std::bad_alloc);
next_construct_fails = false;
Expand All @@ -160,7 +173,8 @@ TEST_CASE("controlled bad_alloc")
{
next_construct_fails = false;
my_json::string_t v("foo");
CHECK_NOTHROW(my_json::json_value j(v));
auto clean_up = [](my_json::json_value& j){ my_allocator_clean_up(j.string); };
CHECK_NOTHROW(my_json::json_value j(v); clean_up(j));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value j(v), std::bad_alloc);
next_construct_fails = false;
Expand Down

0 comments on commit 3f94786

Please sign in to comment.