https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105240

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The report in comment 0 is from libstdc++ which uses a local static variable to
hold the state.

Modifying the comment 1 example to use a global:

#include <backtrace.h>

int cb_pcinfo(void*, uintptr_t, const char*, int, const char* function)
{ return function != nullptr; }

int cb(void* p, uintptr_t pc) { *static_cast<uintptr_t*>(p) = pc; return 1; }

backtrace_state* state;

int main()
{
  state = backtrace_create_state(nullptr, 1, nullptr, nullptr);
  uintptr_t pc;
  backtrace_simple(state, 0, cb, nullptr, &pc);
  backtrace_pcinfo(state, pc, cb_pcinfo, nullptr, nullptr);
}

I get:

==494586== Memcheck, a memory error detector
==494586== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==494586== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==494586== Command: ./a.out
==494586== 
==494586== 
==494586== HEAP SUMMARY:
==494586==     in use at exit: 112,638,181 bytes in 729,676 blocks
==494586==   total heap usage: 729,861 allocs, 185 frees, 115,412,694 bytes
allocated
==494586== 
==494586== 84 bytes in 3 blocks are definitely lost in loss record 6 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x409283: read_lnct (dwarf.c:2543)
==494586==    by 0x409283: read_line_header_format_entries (dwarf.c:2618)
==494586==    by 0x40B08A: read_line_header (dwarf.c:2712)
==494586==    by 0x40B08A: read_line_info (dwarf.c:2965)
==494586==    by 0x40B08A: dwarf_lookup_pc (dwarf.c:3747)
==494586==    by 0x40C1BE: dwarf_fileline (dwarf.c:3935)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 2,839 bytes in 1 blocks are possibly lost in loss record 19 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40667B: elf_add (elf.c:4381)
==494586==    by 0x406D83: backtrace_initialize (elf.c:4877)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 13,920 bytes in 1 blocks are definitely lost in loss record 23 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40568A: elf_get_view (elf.c:426)
==494586==    by 0x40568A: elf_add (elf.c:4329)
==494586==    by 0x406BC8: phdr_callback (elf.c:4848)
==494586==    by 0x4D2A314: dl_iterate_phdr (dl-iteratephdr.c:75)
==494586==    by 0x406DDD: backtrace_initialize (elf.c:4892)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 69,736 bytes in 2 blocks are possibly lost in loss record 26 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40568A: elf_get_view (elf.c:426)
==494586==    by 0x40568A: elf_add (elf.c:4329)
==494586==    by 0x406BC8: phdr_callback (elf.c:4848)
==494586==    by 0x4D2A314: dl_iterate_phdr (dl-iteratephdr.c:75)
==494586==    by 0x406DDD: backtrace_initialize (elf.c:4892)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 451,020 bytes in 7 blocks are possibly lost in loss record 31 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40667B: elf_add (elf.c:4381)
==494586==    by 0x406BC8: phdr_callback (elf.c:4848)
==494586==    by 0x4D2A314: dl_iterate_phdr (dl-iteratephdr.c:75)
==494586==    by 0x406DDD: backtrace_initialize (elf.c:4892)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== LEAK SUMMARY:
==494586==    definitely lost: 14,004 bytes in 4 blocks
==494586==    indirectly lost: 0 bytes in 0 blocks
==494586==      possibly lost: 523,595 bytes in 10 blocks
==494586==    still reachable: 112,100,582 bytes in 729,662 blocks
==494586==         suppressed: 0 bytes in 0 blocks
==494586== Reachable blocks (those to which a pointer was found) are not shown.
==494586== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==494586== 
==494586== For lists of detected and suppressed errors, rerun with: -s
==494586== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

So that's still 14kB definitely lost, 500kB possibly lost.

Reply via email to