Test program with no leaks (testexceptions.cpp): ------------------------------ int main() { try { throw 1; // throw some error (same problem occured when throwing string("aaa")) } catch (int err) { // same problem occured when used catch (...) // do whatever. it doesn't matter } return 0; } -------------------------------------- Compiled with: g++ -o testexceptions-nothread testexceptions.cpp g++ -o testexceptions-thread testexceptions.cpp -lpthread // g++ (GCC) 3.3.6 (PLD Linux) ------------------------------------- Tested with valgrind: export GLIBCPP_FORCE_NEW=1 export GLIBCXX_FORCE_NEW=1 valgrind -v --num-callers=20 --leak-check=full --leak-resolution=high --show-reachable=yes --log-file-exactly=leakinfo testexceptions-thread export -n GLIBCPP_FORCE_NEW export -n GLIBCXX_FORCE_NEW // same error also happened without exporting GLIBC.._FORCE_NEW -------------------------------------------- The version without -lpthread reported no memory leaks but the one with the -lpthread compiling option reports: ==16477== Memcheck, a memory error detector. ==16477== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al. ==16477== Using LibVEX rev 1575, a library for dynamic binary translation. ==16477== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP. ==16477== Using valgrind-3.1.1, a dynamic binary instrumentation framework. ==16477== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al. ==16477== ==16477== My PID = 16477, parent PID = 16476. Prog and args are: ==16477== ./testexceptions2 ==16477== --16477-- --16477-- Command line --16477-- ./testexceptions2 --16477-- Startup, with flags: --16477-- -v --16477-- --num-callers=20 --16477-- --leak-check=full --16477-- --leak-resolution=high --16477-- --show-reachable=yes --16477-- --log-file-exactly=testexceptions2_leakinfo --16477-- Contents of /proc/version: --16477-- Linux version 2.6.14.7-4smp (builder@olimp) (gcc version 3.3.6 (PLD Linux)) #1 SMP Sat Feb 18 17:57:16 CET 2006 --16477-- Arch and subarch: X86, x86-sse2 --16477-- Valgrind library directory: /home/edg/valgrind/valgrind-3.1.1/bin//lib/valgrind --16477-- Reading syms from /lib/ld-2.3.5.so (0x4000000) --16477-- Reading syms from /home/edg/gps_reports/testexceptions2 (0x8048000) --16477-- Reading syms from /home/edg/valgrind/valgrind-3.1.1/bin/lib/valgrind/x86-linux/memcheck (0xB0000000) --16477-- object doesn't have a dynamic symbol table --16477-- Reading suppressions file: /home/edg/valgrind/valgrind-3.1.1/bin//lib/valgrind/default.supp --16477-- REDIR: 0x4010BE0 (index) redirected to 0xB0022572 (vgPlain_x86_linux_REDIR_FOR_index) --16477-- Reading syms from /home/edg/valgrind/valgrind-3.1.1/bin/lib/valgrind/x86-linux/vgpreload_core.so (0x4017000) --16477-- Reading syms from /home/edg/valgrind/valgrind-3.1.1/bin/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x401A000) --16477-- REDIR: 0x4010D80 (strlen) redirected to 0x401D2F0 (strlen) --16477-- Reading syms from /lib/tls/libpthread-2.3.5.so (0x4032000) --16477-- object doesn't have a symbol table --16477-- Reading syms from /usr/lib/libstdc++.so.5.0.7 (0x4044000) --16477-- object doesn't have a symbol table --16477-- Reading syms from /lib/tls/libm-2.3.5.so (0x40FE000) --16477-- object doesn't have a symbol table --16477-- Reading syms from /lib/libgcc_s.so.1 (0x4121000) --16477-- object doesn't have a symbol table --16477-- Reading syms from /lib/tls/libc-2.3.5.so (0x412A000) --16477-- object doesn't have a symbol table --16477-- REDIR: 0x40007B0 (_dl_sysinfo_int80) redirected to 0xB002256F (???) --16477-- REDIR: 0x41916A0 (rindex) redirected to 0x401CF20 (rindex) --16477-- REDIR: 0x4190E00 (strcpy) redirected to 0x401D330 (strcpy) --16477-- REDIR: 0x4192960 (memcpy) redirected to 0x401D660 (memcpy) --16477-- REDIR: 0x41913E0 (strnlen) redirected to 0x401D290 (strnlen) --16477-- REDIR: 0x418C940 (malloc) redirected to 0x401B404 (malloc) --16477-- REDIR: 0x4192460 (memset) redirected to 0x401DAB0 (memset) --16477-- REDIR: 0x4191330 (strlen) redirected to 0x401D2D0 (strlen) --16477-- REDIR: 0x418CAD0 (free) redirected to 0x401BFFD (free) ==16477== ==16477== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 1) --16477-- --16477-- supp: 19 Ubuntu-stripped-ld.so ==16477== malloc/free: in use at exit: 8 bytes in 1 blocks. ==16477== malloc/free: 2 allocs, 1 frees, 92 bytes allocated. ==16477== ==16477== searching for pointers to 1 not-freed blocks. ==16477== checked 130,108 bytes. ==16477== ==16477== 8 bytes in 1 blocks are still reachable in loss record 1 of 1 ==16477== at 0x401B48A: malloc (vg_replace_malloc.c:149) ==16477== by 0x40D532D: __cxa_get_globals (in /usr/lib/libstdc++.so.5.0.7) ==16477== by 0x40D5FFD: __cxa_throw (in /usr/lib/libstdc++.so.5.0.7) ==16477== by 0x804861D: main (in /home/edg/gps_reports/testexceptions2) ==16477== ==16477== LEAK SUMMARY: ==16477== definitely lost: 0 bytes in 0 blocks. ==16477== possibly lost: 0 bytes in 0 blocks. ==16477== still reachable: 8 bytes in 1 blocks. ==16477== suppressed: 0 bytes in 0 blocks. --16477-- memcheck: sanity checks: 6 cheap, 1 expensive --16477-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use --16477-- memcheck: auxmaps: 0 searches, 0 comparisons --16477-- memcheck: secondaries: 11 issued (704k, 0M) --16477-- memcheck: secondaries: 28 accessible and distinguished (1792k, 1M) --16477-- tt/tc: 6,237 tt lookups requiring 6,386 probes --16477-- tt/tc: 6,237 fast-cache updates, 3 flushes --16477-- translate: new 2,997 (63,209 -> 1,010,422; ratio 159:10) [0 scs] --16477-- translate: dumped 0 (0 -> ??) --16477-- translate: discarded 6 (147 -> ??) --16477-- scheduler: 333,831 jumps (bb entries). --16477-- scheduler: 6/3,320 major/minor sched events. --16477-- sanity: 7 cheap, 1 expensive checks. --16477-- exectx: 30,011 lists, 9 contexts (avg 0 per list) --16477-- exectx: 22 searches, 13 full compares (590 per 1000) --16477-- exectx: 0 cmp2, 42 cmp4, 0 cmpAll
The unfreed memory is in a library; Valgrind is doing the right thing here. You could argue that it should be suppressed, but we generally don't bother doing that for still reachable blocks.