c++debuggingtcmallocgoogle-perftools

Why does tcmalloc fail when I compile and run this program with a shared library?


Code simular to code here: Why tcmalloc don't print function name, which provided via dlopen

makefile:

When I execute my program:

$ HEAPCHECK=normal ./a.out
No live heap object at 0x2582aa0 to ignore
Check failed: heap_profile->FindAlloc(test_str, &size): our own new/delete not linked?
Aborted (core dumped)

gdb with core file says:

Core was generated by `./a.out'.
Program terminated with signal SIGABRT, Aborted.
#0  0x00007f51bfef6cc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
Traceback (most recent call last):
  File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
    from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'

Solution

  • It looks like your code that doesn't call malloc/new from main.cpp is causing linked not to link libtcmalloc.so to ./a.out. I.e. you can see it by doing ldd ./a.out. It is not how it's supposed to work.

    As a result of this tcmalloc is loaded together with your shared object which is too late and not supported.

    You can work around this "initiative" (which is as usual, clearly, result of good intentions) by adding -Wl,--no-as-needed before -ltcmalloc when you build your main executable.