[Insight-users] Cannot delete ITK objects

Luis Ibanez luis.ibanez at kitware.com
Fri Oct 9 11:46:11 EDT 2009


Hi Dirk,


You should never call Delete() in an ITK smart pointer.


If you want to destroy the object, simply do:

                      readeritk  = NULL;

and let the ITK smart pointer do the math for determining
whether the object should die or not.

Calling Delete() in ITK smart pointers is a common mistake
when combining VTK code (that requires the use of Delete() )
and ITK code, (that forbids the use of Delete() ).

I would strongly encourage you to use the new
vtkSmartPointer class, so that you can have a unified
mindset when combining ITK and VTK code.

Instead of:

               vtkclass * ptr = vtkclass::New();

do:

 vtkSmartPointer< vtkclass >  ptr = vtkSmartPointer< vtkclass >::New()


and then,
you won't need to call Delete() in VTK classes either.


     Regards,


            Luis


-----------------------------------------------------------------
On Fri, Oct 9, 2009 at 8:36 AM, Dirk Boye <body at nabla.org> wrote:
> Dear list,
>
> When I'm trying to delete an ITK object I get a segmentation fault.
>
> My minimum example test.cpp:
>
> #include <itkImageFileReader.h>
> #include <itkImage.h>
> #include <itkImageFileReader.h>
> #include <itkImageToVTKImageFilter.h>
>
> #include <vtkImageData.h>
>
> #include <cstdio>
>
> typedef float                                   PixelType;
> typedef itk::Image<PixelType, 3>                ImageType;
> typedef itk::ImageFileReader<ImageType>         ReaderType;
> typedef itk::ImageToVTKImageFilter<ImageType>   ConnectorType;
>
> int main(int argc, char **argv){
>        cout << "Test!" << endl;
>        ReaderType::Pointer readeritk = ReaderType::New();
>        readeritk->Delete();
>        vtkImageData *data=vtkImageData::New();
>        data->Delete();
> }
>
> The libraries I link:
> TARGET_LINK_LIBRARIES ( test
>  vtkImaging ITKIO ITKBasicFilters
>  )
>
> If I comment readeritk->Delete();
> everything runs fine.
>
> What am I missing here?
>
> Backtrace:
>
> dirk at dirk-pc:~/projects/test2$ ./test
> Test!
> *** glibc detected *** ./test: double free or corruption (top):
> 0x000000000137ea40 ***
> ======= Backtrace: =========
> /lib/libc.so.6[0x7f1891e08cb8]
> /lib/libc.so.6(cfree+0x76)[0x7f1891e0b276]
> ./test(_ZN3itk11LightObjectD0Ev+0x23e)[0x7b3c10]
> ./test(_ZNK3itk11LightObject10UnRegisterEv+0x48)[0x7b4112]
> ./test(_ZN3itk12SmartPointerINS_15ImageFileReaderINS_5ImageIfLj3EEENS_25DefaultConvertPixelTraitsIfEEEEE10UnRegisterEv+0x35)[0x4fd927]
> ./test(_ZN3itk12SmartPointerINS_15ImageFileReaderINS_5ImageIfLj3EEENS_25DefaultConvertPixelTraitsIfEEEEED1Ev+0x15)[0x4fd93f]
> ./test(main+0x58)[0x4fd414]
> /lib/libc.so.6(__libc_start_main+0xe6)[0x7f1891daf5a6]
> ./test[0x4fd289]
> ======= Memory map: ========
> 00400000-00920000 r-xp 00000000 08:06 291453
> /home/dirk/projects/test2/test
> 00b20000-00b22000 r--p 00520000 08:06 291453
> /home/dirk/projects/test2/test
> 00b22000-00b4c000 rw-p 00522000 08:06 291453
> /home/dirk/projects/test2/test
> 00b4c000-00b4e000 rw-p 00b4c000 00:00 0
> 012b3000-0139a000 rw-p 012b3000 00:00 0
> [heap]
> 7f188c000000-7f188c021000 rw-p 7f188c000000 00:00 0
> 7f188c021000-7f1890000000 ---p 7f188c021000 00:00 0
> 7f1891d91000-7f1891ef9000 r-xp 00000000 08:03 7719
> /lib/libc-2.9.so
> 7f1891ef9000-7f18920f9000 ---p 00168000 08:03 7719
> /lib/libc-2.9.so
> 7f18920f9000-7f18920fd000 r--p 00168000 08:03 7719
> /lib/libc-2.9.so
> 7f18920fd000-7f18920fe000 rw-p 0016c000 08:03 7719
> /lib/libc-2.9.so
> 7f18920fe000-7f1892103000 rw-p 7f18920fe000 00:00 0
> 7f1892103000-7f1892119000 r-xp 00000000 08:03 2587
> /lib/libgcc_s.so.1
> 7f1892119000-7f1892319000 ---p 00016000 08:03 2587
> /lib/libgcc_s.so.1
> 7f1892319000-7f189231a000 r--p 00016000 08:03 2587
> /lib/libgcc_s.so.1
> 7f189231a000-7f189231b000 rw-p 00017000 08:03 2587
> /lib/libgcc_s.so.1
> 7f189231b000-7f189240c000 r-xp 00000000 08:03 9958
> /usr/lib/libstdc++.so.6.0.10
> 7f189240c000-7f189260c000 ---p 000f1000 08:03 9958
> /usr/lib/libstdc++.so.6.0.10
> 7f189260c000-7f1892613000 r--p 000f1000 08:03 9958
> /usr/lib/libstdc++.so.6.0.10
> 7f1892613000-7f1892615000 rw-p 000f8000 08:03 9958
> /usr/lib/libstdc++.so.6.0.10
> 7f1892615000-7f1892628000 rw-p 7f1892615000 00:00 0
> 7f1892628000-7f189262a000 r-xp 00000000 08:03 7722
> /lib/libdl-2.9.so
> 7f189262a000-7f189282a000 ---p 00002000 08:03 7722
> /lib/libdl-2.9.so
> 7f189282a000-7f189282b000 r--p 00002000 08:03 7722
> /lib/libdl-2.9.so
> 7f189282b000-7f189282c000 rw-p 00003000 08:03 7722
> /lib/libdl-2.9.so
> 7f189282c000-7f18928b0000 r-xp 00000000 08:03 7723
> /lib/libm-2.9.so
> 7f18928b0000-7f1892aaf000 ---p 00084000 08:03 7723
> /lib/libm-2.9.so
> 7f1892aaf000-7f1892ab0000 r--p 00083000 08:03 7723
> /lib/libm-2.9.so
> 7f1892ab0000-7f1892ab1000 rw-p 00084000 08:03 7723
> /lib/libm-2.9.so
> 7f1892ab1000-7f1892ac8000 r-xp 00000000 08:03 7733
> /lib/libpthread-2.9.so
> 7f1892ac8000-7f1892cc7000 ---p 00017000 08:03 7733
> /lib/libpthread-2.9.so
> 7f1892cc7000-7f1892cc8000 r--p 00016000 08:03 7733
> /lib/libpthread-2.9.so
> 7f1892cc8000-7f1892cc9000 rw-p 00017000 08:03 7733
> /lib/libpthread-2.9.so
> 7f1892cc9000-7f1892ccd000 rw-p 7f1892cc9000 00:00 0
> 7f1892ccd000-7f1892cd0000 r-xp 00000000 08:03 2676
> /lib/libuuid.so.1.2
> 7f1892cd0000-7f1892ed0000 ---p 00003000 08:03 2676
> /lib/libuuid.so.1.2
> 7f1892ed0000-7f1892ed1000 r--p 00003000 08:03 2676
> /lib/libuuid.so.1.2
> 7f1892ed1000-7f1892ed2000 rw-p 00004000 08:03 2676
> /lib/libuuid.so.1.2
> 7f1892ed2000-7f1892ef2000 r-xp 00000000 08:03 7716
> /lib/ld-2.9.so
> 7f18930cb000-7f18930cf000 rw-p 7f18930cb000 00:00 0
> 7f18930ed000-7f18930f1000 rw-p 7f18930ed000 00:00 0
> 7f18930f1000-7f18930f2000 r--p 0001f000 08:03 7716
> /lib/ld-2.9.so
> 7f18930f2000-7f18930f3000 rw-p 00020000 08:03 7716
> /lib/ld-2.9.so
> 7fff9b0de000-7fff9b0f3000 rw-p 7ffffffea000 00:00 0
> [stack]
> 7fff9b1ff000-7fff9b200000 r-xp 7fff9b1ff000 00:00 0
> [vdso]
> ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
> [vsyscall]
> Aborted
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ITK FAQ at: http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list