From ecaac22656ee4088be4bee505862bee2799e0788 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 15 Jul 2021 20:34:50 +0200 Subject: [PATCH] :bulb: add comment to describe j.m_value.destroy(j.m_type) calls --- include/nlohmann/detail/conversions/to_json.hpp | 7 +++++++ single_include/nlohmann/json.hpp | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 8d1ac30c5..08462a4bb 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -22,6 +22,13 @@ namespace detail // constructors // ////////////////// +/* + * Note all external_constructor<>::construct functions need to call + * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an + * allocated value (e.g., a string). See bug issue + * https://github.com/nlohmann/json/issues/2865 for more information. + */ + template struct external_constructor; template<> diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 32cacdbcd..dedd3d4a5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4480,6 +4480,13 @@ namespace detail // constructors // ////////////////// +/* + * Note all external_constructor<>::construct functions need to call + * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an + * allocated value (e.g., a string). See bug issue + * https://github.com/nlohmann/json/issues/2865 for more information. + */ + template struct external_constructor; template<>