[Insight-users] ConnectedThresholdImageFilter does takeintoaccopunt the configuration

Michael Jackson mike.jackson at bluequartz.net
Tue Oct 13 11:50:23 EDT 2009


In Debug mode maybe the compiler is initializing variables for you or  
checking that you are not accessing deallocated pointers and in  
release mode the compiler does different things. Someone who knows  
more about Visual Studio compilers than me will have to answer that  
question.

Try this:

TItkImage* ptr = l_pImportFilter->GetOutput();
std::cout << "ref Count: " << ptr->GetReferenceCount() << std::endl;
ptr->Register(); // will increment the reference count
std::cout << "ref Count: " << ptr->GetReferenceCount() << std::endl;

You should see the value incremented if _I_ understand the code  
correctly..

--
Mike Jackson <www.bluequartz.net>

On Oct 13, 2009, at 11:37 AM, Daanen Vincent wrote:

> I already tried the first solution you proposed (call Register())  
> but it
> changes nothing :(
> The second solution does not work either ..
>
> Btw, if you're right, with regard to smartPointer behavior, how can  
> you
> explain that my code is ok in debug mode ? I could check value of the
> wrapped pointer in the imagewriter so after the call to the  
> destructor of
> the importfilter ...
>
> I really do not understand why the behaviors are so different in  
> debug and
> release mode
>
> 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:43
>> À : Insight Users Mailing List
>> Objet : Re: [Insight-users] ConnectedThresholdImageFilter
>> does takeintoaccopunt the configuration
>>
>> 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
>>>
>>
>> _____________________________________
>> 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