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 _start
  • @ 0_init

foo 関数で line ではなくアドレス 0x00007fe3460bd9ba を取得するのはなぜですか? 助けてください

Ps LD_PRELOAD=.../tcmalloc.so で gdb を使用しようとしましたが、次のメッセージが表示されます。

4

1 に答える 1

0

dlclose 呼び出しを削除してみてください。

ヒープ チェッカーとプロファイラーがアンロードされた共有オブジェクトを処理できないという既知の問題があります。

于 2015-07-01T15:59:42.433 に答える