问题
I have next some project: 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;
}
shared.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;
}
}
and Makefile:
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
I use tcmalloc for debug this test program, which load dynamically libshared.so:foo and execute it.run command: LD_PRELOAD=/usr/local/lib/libtcmalloc.so HEAPCHECK=normal ./a.out
The 1 largest leaks:
- Using local file ./a.out.
- Leak of 40 bytes in 1 objects allocated from:
- @ 7fe3460bd9ba 0x00007fe3460bd9ba
- @ 400b43 main
- @ 7fe346c33ec5 __libc_start_main
- @ 400999 _start
- @ 0 _init
Why I get address 0x00007fe3460bd9ba instead of line in foo function? please help
P.s. I tried to use gdb with LD_PRELOAD=.../tcmalloc.so, but I get: "Someone is ptrace()ing us; will turn itself off Turning perftools heap leak checking off"
回答1:
Try removing dlclose call.
It's known issue that heap checker & profilers can't handle unloaded shared objects.
来源:https://stackoverflow.com/questions/31166005/why-tcmalloc-dont-print-function-name-which-provided-via-dlopen