[Insight-users] memory leak check in ITK?

Feng Ma mafeng at hotmail . com
Fri, 26 Sep 2003 21:19:06 -0400


Hi, Luis:

  OUTPUT_MEM_BORROW means that the caller function will borrow image memory 
from ITK image class and is responsible to release it. It is not used in my 
code because the caller is a C function.

  I commented out the suspicious code and re-run valgrind. I have similiar 
results. Then I run example ResampleImageFilter2. I have similiar memory 
leak too. One difference is that the leak size is 16. I guess it is due to 
image dimension difference.

  What do you think about the other 16 bytes leak? Seems that it is not 
related to either ITK or my code. Just normal c++ issue?

  I put valgrind results of ResampleImageFilter2 here:

valgrind --error-limit=no --leak-check=yes -v --show-reachable=yes 
--num-callers=50 ./ResampleImageFilter2 BrainProtonDensitySlice.png 
resample2.png 0 >& resample2.txt

==25811== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
==25811== Copyright (C) 2002-2003, and GNU GPL'd, by Julian Seward.
==25811== Using valgrind-20030725, a program supervision framework for 
x86-linux.
==25811== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
==25811== Startup, with flags:
==25811==    --suppressions=/usr/local/lib/valgrind/default.supp
==25811==    --error-limit=no
==25811==    --leak-check=yes
==25811==    -v
==25811==    --show-reachable=yes
==25811==    --num-callers=50
==25811== Reading syms from 
/r2net/r2/fma/ITKTest/test/bin/ResampleImageFilter2
==25811== Reading syms from /lib/ld-2.2.4.so
==25811== Reading syms from /usr/local/lib/valgrind/vgskin_memcheck.so
==25811== Reading syms from /usr/local/lib/valgrind/valgrind.so
==25811== Reading syms from /usr/local/lib/valgrind/libpthread.so
==25811== Reading syms from /lib/libdl-2.2.4.so
==25811== Reading syms from /lib/i686/libm-2.2.4.so
==25811== Reading syms from /usr/local/lib/libstdc++.so.5.0.5
==25811== Reading syms from /usr/local/lib/libgcc_s.so.1
==25811== Reading syms from /lib/i686/libc-2.2.4.so
==25811== Reading suppressions file: /usr/local/lib/valgrind/default.supp
==25811== Estimated CPU clock rate is 998 MHz
==25811==
==25811==
==25811== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 18 from 2)
--25811--
--25811-- supp:   10 __pthread_mutex_unlock/_IO_funlockfile
--25811-- supp:    8 
pthread_error/__pthread_mutex_destroy/_IO_default_finish
==25811== malloc/free: in use at exit: 5176 bytes in 9 blocks.
==25811== malloc/free: 194 allocs, 185 frees, 751813 bytes allocated.
==25811==
==25811== searching for pointers to 9 not-freed blocks.
==25811== checked 6068952 bytes.
==25811==
==25811== 16 bytes in 2 blocks are definitely lost in loss record 1 of 5
==25811==    at 0x4002CB02: __builtin_vec_new (vg_replace_malloc.c:197)
==25811==    by 0x4002CB6D: operator new[](unsigned) 
(vg_replace_malloc.c:210)
==25811==    by 0x8134806: vnl_c_vector_alloc(int, int) 
(/space2/Devel/InsightToolkit-1.4.0/Utilities/vxl/vnl/vnl_c_vector.txx:362)
==25811==    by 0x81346DC: vnl_c_vector<double>::allocate_Tptr(int) 
(/space2/Devel/InsightToolkit-1.4.0/Utilities/vxl/vnl/vnl_c_vector.txx:383)
==25811==    by 0x80FB0E6: vnl_matrix_ref<double>::vnl_matrix_ref(unsigned, 
unsigned, double*) 
(/space2/Devel/InsightToolkit-1.4.0/Utilities/vxl/vnl/vnl_matrix_ref.h:46)
==25811==    by 0x80FB0B0: vnl_matrix_fixed<double, 2, 2>::operator 
vnl_matrix_ref<double> const() const 
(/space2/Devel/InsightToolkit-1.4.0/Utilities/vxl/vnl/vnl_matrix_fixed.h:533)
==25811==    by 0x80FAF67: itk::Matrix<double, 2, 2>::GetInverse() const 
(/space2/Devel/InsightToolkit-1.4.0/Code/Common/itkMatrix.txx:204)
==25811==    by 0x80F732F: itk::AffineTransform<double, 
2>::RecomputeInverse() 
(/space2/Devel/InsightToolkit-1.4.0/Code/Common/itkAffineTransform.txx:591)
==25811==    by 0x80F691A: itk::AffineTransform<double, 2>::SetIdentity() 
(/space2/Devel/InsightToolkit-1.4.0/Code/Common/itkAffineTransform.h:193)
==25811==    by 0x809979F: main 
(/space2/Devel/InsightToolkit-1.4.0/Examples/Filtering/ResampleImageFilter2.cxx:178)
==25811==    by 0x40387656: __libc_start_main 
(../sysdeps/generic/libc-start.c:129)
==25811==    by 0x80993A0: (within 
/r2net/r2/fma/ITKTest/test/bin/ResampleImageFilter2)
==25811==
==25811==
==25811== 16 bytes in 1 blocks are definitely lost in loss record 2 of 5
==25811==    at 0x4002C90D: malloc (vg_replace_malloc.c:153)
==25811==    by 0x4002CE92: realloc (vg_replace_malloc.c:291)
==25811==    by 0x403F5064: __argz_append (argz-append.c:30)
==25811==    by 0x403923D6: __newlocale (newlocale.c:100)
==25811==    by 0x402F59AD: 
std::locale::facet::_S_create_c_locale(__locale_struct*&, char const*, 
__locale_struct*) (c++locale.cc:171)
==25811==    by 0x402ED139: std::locale::_Impl::_Impl(std::locale::facet**, 
unsigned, bool) (../../../../gcc-3.3.1/libstdc++-v3/src/localename.cc:226)
==25811==    by 0x402EA0E4: std::locale::classic() 
(../../../../gcc-3.3.1/libstdc++-v3/libsupc++/new:92)
==25811==    by 0x402E8BAC: std::locale::locale() 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/locale_classes.h:179)
==25811==    by 0x402FC636: std::basic_filebuf<char, std::char_traits<char> 
 >::basic_filebuf() 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/streambuf:583)
==25811==    by 0x402FBAA1: __gnu_cxx::stdio_filebuf<char, 
std::char_traits<char> >::stdio_filebuf(_IO_FILE*, std::_Ios_Openmode, 
unsigned) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/ext/stdio_filebuf.h:147)
==25811==    by 0x402E7DCF: std::ios_base::Init::_S_ios_create(bool) 
(../../../../gcc-3.3.1/libstdc++-v3/libsupc++/new:92)
==25811==    by 0x402E8464: std::ios_base::Init::Init() 
(../../../../gcc-3.3.1/libstdc++-v3/src/ios.cc:228)
==25811==    by 0x402E7B76: __static_initialization_and_destruction_0(int, 
int) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/iostream:77)
==25811==    by 0x402E7B99: 
_GLOBAL__I__ZThn8_NSt14basic_iostreamIwSt11char_traitsIwEED0Ev.._.._.._.._gcc_3.3.1_libstdc___v3_src_io_inst.ccl5ZUua 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/locale_facets.h:210)
==25811==    by 0x402E5B84: (within /usr/local/lib/libstdc++.so.5.0.5)
==25811==    by 0x402DE83D: (within /usr/local/lib/libstdc++.so.5.0.5)
==25811==    by 0x4000DAA6: _dl_init (dl-init.c:70)
==25811==    by 0x40001ED0: (within /lib/ld-2.2.4.so)
==25811==
==25811==
==25811== 64 bytes in 1 blocks are still reachable in loss record 3 of 5
==25811==    at 0x4002C90D: malloc (vg_replace_malloc.c:153)
==25811==    by 0x403926BC: __newlocale (newlocale.c:162)
==25811==    by 0x402F59AD: 
std::locale::facet::_S_create_c_locale(__locale_struct*&, char const*, 
__locale_struct*) (c++locale.cc:171)
==25811==    by 0x402ED139: std::locale::_Impl::_Impl(std::locale::facet**, 
unsigned, bool) (../../../../gcc-3.3.1/libstdc++-v3/src/localename.cc:226)
==25811==    by 0x402EA0E4: std::locale::classic() 
(../../../../gcc-3.3.1/libstdc++-v3/libsupc++/new:92)
==25811==    by 0x402E8BAC: std::locale::locale() 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/locale_classes.h:179)
==25811==    by 0x402FC636: std::basic_filebuf<char, std::char_traits<char> 
 >::basic_filebuf() 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/streambuf:583)
==25811==    by 0x402FBAA1: __gnu_cxx::stdio_filebuf<char, 
std::char_traits<char> >::stdio_filebuf(_IO_FILE*, std::_Ios_Openmode, 
unsigned) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/ext/stdio_filebuf.h:147)
==25811==    by 0x402E7DCF: std::ios_base::Init::_S_ios_create(bool) 
(../../../../gcc-3.3.1/libstdc++-v3/libsupc++/new:92)
==25811==    by 0x402E8464: std::ios_base::Init::Init() 
(../../../../gcc-3.3.1/libstdc++-v3/src/ios.cc:228)
==25811==    by 0x402E7B76: __static_initialization_and_destruction_0(int, 
int) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/iostream:77)
==25811==    by 0x402E7B99: 
_GLOBAL__I__ZThn8_NSt14basic_iostreamIwSt11char_traitsIwEED0Ev.._.._.._.._gcc_3.3.1_libstdc___v3_src_io_inst.ccl5ZUua 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/locale_facets.h:210)
==25811==    by 0x402E5B84: (within /usr/local/lib/libstdc++.so.5.0.5)
==25811==    by 0x402DE83D: (within /usr/local/lib/libstdc++.so.5.0.5)
==25811==    by 0x4000DAA6: _dl_init (dl-init.c:70)
==25811==    by 0x40001ED0: (within /lib/ld-2.2.4.so)
==25811==
==25811==
==25811== 4880 bytes in 4 blocks are still reachable in loss record 5 of 5
==25811==    at 0x4002C9FD: __builtin_new (vg_replace_malloc.c:172)
==25811==    by 0x4002CA68: operator new(unsigned) (vg_replace_malloc.c:185)
==25811==    by 0x40332B2A: std::__default_alloc_template<true, 
0>::_S_chunk_alloc(unsigned, int&) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/stl_alloc.h:108)
==25811==    by 0x40332A3C: std::__default_alloc_template<true, 
0>::_S_refill(unsigned) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/stl_alloc.h:550)
==25811==    by 0x40332737: std::__default_alloc_template<true, 
0>::allocate(unsigned) 
(/r2/fma/gcc331Build/i686-pc-linux-gnu/libstdc++-v3/include/bits/stl_alloc.h:357)
==25811==    by 0x81135A4: 
std::__simple_alloc<std::_List_node<itk::ObjectFactoryBase*>, 
std::__default_alloc_template<true, 0> >::allocate(unsigned) 
(/usr/local/include/c++/3.3.1/bits/stl_alloc.h:232)
==25811==    by 0x8113577: std::_List_alloc_base<itk::ObjectFactoryBase*, 
std::allocator<itk::ObjectFactoryBase*>, true>::_M_get_node() 
(/usr/local/include/c++/3.3.1/bits/stl_list.h:275)
==25811==    by 0x811353E: std::_List_base<itk::ObjectFactoryBase*, 
std::allocator<itk::ObjectFactoryBase*> 
 >::_List_base(std::allocator<itk::ObjectFactoryBase*> const&) 
(/usr/local/include/c++/3.3.1/bits/stl_list.h:304)
==25811==    by 0x81129C1: std::list<itk::ObjectFactoryBase*, 
std::allocator<itk::ObjectFactoryBase*> 
 >::list(std::allocator<itk::ObjectFactoryBase*> const&) 
(/usr/local/include/c++/3.3.1/bits/stl_list.h:452)
==25811==    by 0x80A0036: itk::ObjectFactoryBase::Initialize() 
(/space2/Devel/InsightToolkit-1.4.0/Code/Common/itkObjectFactoryBase.cxx:171)
==25811==    by 0x809FD98: itk::ObjectFactoryBase::CreateInstance(char 
const*) 
(/space2/Devel/InsightToolkit-1.4.0/Code/Common/itkObjectFactoryBase.cxx:115)
==25811==    by 0x80F695B: 
itk::ObjectFactory<itk::ImageFileReader<itk::Image<unsigned char, 2>, 
itk::DefaultConvertPixelTraits<unsigned char> > >::Create() 
(/space2/Devel/InsightToolkit-1.4.0/Code/Common/itkObjectFactory.h:52)
==25811==    by 0x80F6338: itk::ImageFileReader<itk::Image<unsigned char, 
2>, itk::DefaultConvertPixelTraits<unsigned char> >::New() 
(/space2/Devel/InsightToolkit-1.4.0/Code/IO/itkImageFileReader.h:97)
==25811==    by 0x8099567: main 
(/space2/Devel/InsightToolkit-1.4.0/Examples/Filtering/ResampleImageFilter2.cxx:87)
==25811==    by 0x40387656: __libc_start_main 
(../sysdeps/generic/libc-start.c:129)
==25811==    by 0x80993A0: (within 
/r2net/r2/fma/ITKTest/test/bin/ResampleImageFilter2)
==25811==
==25811== LEAK SUMMARY:
==25811==    definitely lost: 32 bytes in 3 blocks.
==25811==    possibly lost:   0 bytes in 0 blocks.
==25811==    still reachable: 4944 bytes in 5 blocks.
==25811==         suppressed: 200 bytes in 1 blocks.
==25811==
--25811--     TT/TC: 0 tc sectors discarded.
--25811--            7555 chainings, 0 unchainings.
--25811-- translate: new     11901 (195746 -> 2586008; ratio 132:10)
--25811--            discard 0 (0 -> 0; ratio 0:10).
--25811--  dispatch: 36000000 jumps (bb entries), of which 11572692 (32%) 
were unchained.
--25811--            723/23356 major/minor sched events.  12979 tt_fast 
misses.
--25811-- reg-alloc: 2129 t-req-spill, 476031+13395 orig+spill uis, 58579 
total-reg-r.
--25811--    sanity: 724 cheap, 29 expensive checks.
--25811--    ccalls: 59829 C calls, 57% saves+restores avoided (201688 
bytes)
--25811--            79747 args, avg 0.87 setup instrs each (20042 bytes)
--25811--            0% clear the stack (179487 bytes)
--25811--            23303 retvals, 26% of reg-reg movs avoided (12046 
bytes)


Thanks and have a great weekend.

-Feng

_________________________________________________________________
Add MSN 8 Internet Software to your existing Internet access and enjoy 
patented spam protection and more.  Sign up now!   
http://join . msn . com/?page=dept/byoa