[Insight-users] ConnectedThresholdImageFilter does take intoaccopunt the configuration

Michael Jackson mike.jackson at bluequartz.net
Tue Oct 13 10:42:38 EDT 2009


Um, No.

  A "Smart Pointer" is another C++ object that "wraps" around an  
actual pointer and usually performs reference counting for that  
pointer. So TItkImage::Pointer and TItkImage* are very Different  
objects.

TItkImage::Pointer is the smart pointer. TItkImage* is what it is  
wrapping.

If you look at the itkImportImageFilter source code you will see the  
following:

typedef ImportImageFilter             Self;
typedef SmartPointer<Self>            Pointer;


If you then look at the itkSmartPointer source you will see the  
following:

   /** Access function to pointer. */
   ObjectType *GetPointer () const
     { return m_Pointer; }

So in order to get at the "raw" pointer you would need the following:
TItkImage::Pointer smPtr = TItkImage::New();
TItkImage* ptr = smPtr.GetPointer();


Here is where "Ownership" comes into play. The 'l_pImportFilter'  
object "owns" the output that it creates. When your method returns,  
the 'l_pImportFilter' variable goes out of scope, the destructor is  
called. During the destructor the reference count is checked and found  
to be 1 so the pointer is "deleted", thus destroying the 'ptr'  
variable in the above code.

So, looking through the itkSmartPointer source you may be able to do  
the following:

TItkImage* ptr = l_pImportFilter->GetOutput();
ptr->Register(); // will increment the reference count

This _should_ stop the pointer from being deleted when the  
SmartPointer destructor is called.

BUT be _very_ careful with that now potentially dangling pointer. YOU  
Must now explicitly call 'ptr->UnRegister()' in order to clean up the  
memory when you are done with the memory or you will get a memory leak.

You could also construct another SmartPointer to hold that raw pointer  
and perform the necessary reference counting for you.

TItkImage::Pointer smartPtr = TItkImage::New();
smartPtr = ptr;

then return the 'smartPtr' variable.


_________________________________________________________
Mike Jackson                  mike.jackson at bluequartz.net
BlueQuartz Software                    www.bluequartz.net
Principal Software Engineer                  Dayton, Ohio

On Oct 13, 2009, at 10:23 AM, Daanen Vincent wrote:

> Umh, I do not agree with you
>
> TItkImage::Pointer l_pItkImg = l_pImportFilter->GetOutput();
> return l_pItkImg;
>
> Somewhere, there is something like this:
> TItkInterface::TItkImage::Pointer l_pImg =
> TItkInterface::Gmcao2Itk(*l_pImg);
>
> So, according to me, the SmartPtr, l_pImg = l_pItkImg and thus the  
> reference
> count of l_pImg should be the reference count of l_pItkImg + 1 and  
> thus it
> shoukld not be deleted at the exit of the method.
> That's the way I understand smart pointer ...
>
> V
>
>> -----Message d'origine-----
>> De : insight-users-bounces at itk.org
>> [mailto:insight-users-bounces at itk.org] De la part de Michael Jackson
>> Envoyé : mardi 13 octobre 2009 16:17
>> À : Insight Users Mailing List
>> Objet : Re: [Insight-users] ConnectedThresholdImageFilter
>> does take intoaccopunt the configuration
>>
>>
>>
>> On Oct 13, 2009, at 9:16 AM, Daanen Vincent wrote:
>>
>>> TItkImage::Pointer l_pItkImg = l_pImportFilter->GetOutput();
>>
>> I don't think that line is correct..
>>
>> That line should be:
>>
>> TItkImage* l_pItkImg = l_pImportFilter->GetOutput();
>>
>> and when you leave the method that pointer will be deleted by the
>> smart pointer that is wrapping it. I would instead actually
>> return the
>> l_pImportFilter instead. Then in the calling code you can get at the
>> raw pointer, or buffer or what ever it is that you need from it.
>>
>> _________________________________________________________
>> Mike Jackson                  mike.jackson at bluequartz.net
>> BlueQuartz Software                    www.bluequartz.net
>> Principal Software Engineer                  Dayton, Ohio
>> _____________________________________
>> 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