1

我有下一个项目:main.cpp

#include <iostream>
#include <cstddef>
#include <dlfcn.h>

int main()
{
    void* handle = dlopen("./shared_libs/libshared.so", RTLD_LAZY);
    if (NULL == handle)
    {
        std::cerr << "Cannot open library: " << dlerror() << '\n';
        return -1;
    }


    typedef int (*foo_t)(const std::size_t);
    foo_t foo = reinterpret_cast<foo_t>(dlsym(handle, "foo"));


    const char* dlsym_error = dlerror();
    if (dlsym_error)
    {
        std::cerr << "Cannot load symbol 'foo': " << dlsym_error << '\n';
        dlclose(handle);
        return -2;
    }


    std::cout << "call foo" << std::endl;
    foo(10);


    dlclose(handle);


    return 0;
}

共享.cpp:

#include <cstddef>
#include <iostream>


extern "C"
{
    int foo(const std::size_t size)
    {
        int b = size / size;
        int* a = new int[size];
        std::cout << "leaky code here" << std::endl;
    }
}

和生成文件:

all:
    g++ -fPIC -g -c shared.cpp
    g++ -shared -o shared_libs/libshared.so -g shared.o
    g++ -L shared_libs/ -g main.cpp -ldl

我使用 tcmalloc 调试这个测试程序,它动态加载 libshared.so:foo 并执行 it.run 命令: LD_PRELOAD=/usr/local/lib/libtcmalloc.so HEAPCHECK=normal ./a.out

1 最大的泄漏:

  • 使用本地文件 ./a.out。
  • 从以下位置分配的 1 个对象中泄漏 40 个字节:
  • @ 7fe3460bd9ba 0x00007fe3460bd9ba
  • @ 400b43 主要
  • @ 7fe346c33ec5 __libc_start_main
  • @ 400999 _开始
  • @0 _init

为什么我在 foo 函数中得到地址 0x00007fe3460bd9ba 而不是 line?请帮忙

Ps 我尝试将 gdb 与 LD_PRELOAD=.../tcmalloc.so 一起使用,但我得到:“有人正在 ptrace() 向我们发送消息;将自行关闭关闭 perftools 堆泄漏检查”

4

1 回答 1

0

尝试删除 dlclose 调用。

已知问题是堆检查器和分析器无法处理卸载的共享对象。

于 2015-07-01T15:59:42.433 回答