diff --git a/src/json.hpp b/src/json.hpp index 5b4bec8a..86c66f87 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -7944,12 +7944,15 @@ class basic_json static T* create(Args&& ... args) { AllocatorType alloc; + + using AllocatorTraits = std::allocator_traits>; + auto deleter = [&](T * object) { - alloc.deallocate(object, 1); + AllocatorTraits::deallocate(alloc, object, 1); }; - std::unique_ptr object(alloc.allocate(1), deleter); - alloc.construct(object.get(), std::forward(args)...); + std::unique_ptr object(AllocatorTraits::allocate(alloc, 1), deleter); + AllocatorTraits::construct(alloc, object.get(), std::forward(args)...); assert(object != nullptr); return object.release(); } @@ -8937,6 +8940,39 @@ class basic_json ~basic_json() { assert_invariant(); + + switch (m_type) + { + case value_t::object: + { + AllocatorType alloc; + std::allocator_traits::destroy(alloc, m_value.object); + std::allocator_traits::deallocate(alloc, m_value.object, 1); + break; + } + + case value_t::array: + { + AllocatorType alloc; + std::allocator_traits::destroy(alloc, m_value.array); + std::allocator_traits::deallocate(alloc, m_value.array, 1); + break; + } + + case value_t::string: + { + AllocatorType alloc; + std::allocator_traits::destroy(alloc, m_value.string); + std::allocator_traits::deallocate(alloc, m_value.string, 1); + break; + } + + default: + { + // all other types need no specific destructor + break; + } + } m_value.destroy(m_type); }