From 68b49deed3d92e27abcc418400dc0e9c821481b6 Mon Sep 17 00:00:00 2001 From: bergmann Date: Sat, 23 Nov 2019 01:17:37 +0100 Subject: [PATCH] * Fixed memory leak in type_helper --- include/cppcore/misc/type_helper.h | 2 +- include/cppcore/misc/type_helper.inl | 29 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/include/cppcore/misc/type_helper.h b/include/cppcore/misc/type_helper.h index d580639..77e50f2 100644 --- a/include/cppcore/misc/type_helper.h +++ b/include/cppcore/misc/type_helper.h @@ -16,7 +16,7 @@ namespace cppcore /** * @brief Get the name of the type. */ - static inline std::string name(); + static inline const std::string& name(); }; diff --git a/include/cppcore/misc/type_helper.inl b/include/cppcore/misc/type_helper.inl index 75dfa23..ec9fe73 100644 --- a/include/cppcore/misc/type_helper.inl +++ b/include/cppcore/misc/type_helper.inl @@ -1,16 +1,37 @@ #pragma once +#include + #include "type_helper.h" namespace cppcore { + namespace __impl + { + + template + std::string get_type_name() + { + using ptr_u = std::unique_ptr; + + int status; + ptr_u name( + abi::__cxa_demangle(typeid(T).name(), 0, 0, &status), + &free); + + return std::string(name + ? name.get() + : typeid(T).name()); + } + + } + template - std::string type_helper::name() + const std::string& type_helper::name() { - int status; - auto name = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status); - return std::string(name ? name : typeid(T).name()); + static auto value = __impl::get_type_name(); + return value; } /**