abi :: __ cxa_demangle无法重用自身返回的内存

时间:2015-10-01 09:56:20

标签: c++ gcc memory memory-management clang

我尝试使用abi::__cxa_demangle来解码用户定义的类型:

#include <iostream>
#include <mutex>
#include <memory>
#include <string>
#include <typeinfo>

#include <cassert>
#include <cstdlib>

#include <cxxabi.h>

namespace
{

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
std::mutex m;
std::unique_ptr< char, decltype(std::free) & > demangled_name{nullptr, std::free};
#pragma clang diagnostic pop

}

inline
std::string
get_demangled_name(char const * const symbol) noexcept
{
    if (!symbol) {
        return "<null>";
    }
    std::lock_guard< std::mutex > lock(m);
    int status = -4;
    demangled_name.reset(abi::__cxa_demangle(symbol, demangled_name.release(), nullptr, &status));
    return ((status == 0) ? demangled_name.get() : symbol);
}

template< typename ...types >
void
f(std::size_t const i)
{
    using F = void (*)();
    assert(i < sizeof...(types));
    static F const a[sizeof...(types)] = {static_cast< F >([] () { std::cout << get_demangled_name(typeid(types).name()) << std::endl; })...};
    return a[i]();
};

struct A {};
struct B {};
struct X {};
struct Y {};
struct Z {};

int
main()
{
    for (std::size_t i = 0; i < 5; ++i) {
       f< A, B, X, Y, Z >(i);
    }
    return EXIT_SUCCESS;
}

abi::__cxa_demangle每隔一次返回status "-3: One of the arguments is invalid."

第一次通话时(A),智能指针包含nullptrabi::__cxa_demangle返回零status "0: The demangling operation succeeded."。但文件说:

  

output_buffer:一个内存区域,用malloc分配,长度为* length字节,存储的是demangled名称。如果output_buffer不够长,则使用realloc进行扩展。 output_buffer可能改为NULL;在这种情况下,demangled名称放在用malloc分配的内存区域中。

因此,我得出结论,该函数无法重用自身一致分配的内存。是错误还是仅仅是我对文档的误解?

2 个答案:

答案 0 :(得分:2)

你误解了文档:

  

output_buffer:一个内存区域,用malloc分配,* length bytes

您正在传递使用malloc分配的内存区域,但length为空,因此未定义*length

为了知道它是否可以重新使用内存,它需要知道块的大小,所以你需要传入长度作为第三个参数。

GCC中的实现(实际上在libiberty support library中)确实:

  if (output_buffer != NULL && length == NULL)
    {
      if (status != NULL)
        *status = -3;
      return NULL;
    }

因此,如果传递非空output_buffer指针,则还必须传递非空length指针。

由于您不知道分配的块有多大,因此使用当前代码可以使用strlen(demangled_name.get())+1来查找明确分配的最小长度。

最好保留一个存储先前大小的size_t全局,然后将其传入。你应该把它全部包含在一个类中,而不仅仅是一堆全局变量。

答案 1 :(得分:0)

要在gcc下解码,请使用此代码。它很可靠。

#include <cxxabi.h>
#include <memory>
#include <string>
#include <cassert>

template <typename T>
std::string demangle(T& e)
{
    int status;
    std::unique_ptr<char> realname;
    const std::type_info  &ti = typeid(e);
    realname.reset(abi::__cxa_demangle(ti.name(), 0, 0, &status));
    assert(status == 0);
    return std::string(realname.get());
}