Skip to content

Commit

Permalink
(Releng) Fix memory leak when __cxa_demangle is called
Browse files Browse the repository at this point in the history
  • Loading branch information
Bourgoin Thomas committed Jul 13, 2021
1 parent e21b675 commit ad362ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gegelatilib/include/data/arrayWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
#define DEMANGLE_TYPEID_NAME(name) name
#elif __GNUC__
#include <cxxabi.h>
std::string demangle(const char* name);
/// Macro for getting type name in human readable format.
#define DEMANGLE_TYPEID_NAME(name) \
abi::__cxa_demangle(name, nullptr, nullptr, nullptr)
demangle(name).c_str()
#else
#error Unsupported compiler (yet): Check need for name demangling of typeid.name().
#endif
Expand Down
20 changes: 20 additions & 0 deletions gegelatilib/src/data/demangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "data/arrayWrapper.h"

#ifdef __GNUC__
std::string demangle(const char* name)
{
int status = -4;
char* demangleValue = abi::__cxa_demangle(name, nullptr, nullptr, &status);
std::string result;
if (status == 0) {
result = std::string{demangleValue};
free(demangleValue);
}
else {
throw std::runtime_error("Error while trying to demangle the value. "
"Return code of abi::__cxa_demangle is " +
std::to_string(status));
}
return result;
}
#endif

0 comments on commit ad362ae

Please sign in to comment.