[Insight-users] Retain the output of filters using Smart Pointers

Luis Ibanez luis.ibanez at kitware.com
Tue Jun 8 18:51:54 EDT 2004


Hi Jayme,

The reference count is made by the object itself.

When you take the output of the filter and assign it
to a SmartPointer, the Reference count of the output
image gets incremented by one. Like in:

    ImageType::Pointer image = filter->GetOutput();

When the filter is destroyed, the reference count
of the image is decremented by one.

A simple way to double check this, and in the way
have some fun, is to print out the value of the
reference count.

Try the following:

    ImageType::Pointer image;
    {
      FilterType::Pointer filter = FilterType::New();
      filter->SetInput( someInputImage );
      filter->Update();
      std::cout << filter->GetOutput()->GetReferenceCount() << std::endl;
      image = filter->GetOutput();
      std::cout << filter->GetOutput()->GetReferenceCount() << std::endl;
   }
   std::cout << image->GetReferenceCount() << std::endl;



As output you should get the values { 1, 2, 1 }

For your particular code example, you are doing a
safe operation.


Regards,


    Luis


---------------------
Jayme Kosior wrote:

> Hello.
> 
> I want to retain the output of a filter using a smart pointer.  Here is 
> the following pseudo-code:
> 
> // Create smart pointer
> SmartPointer s_ptr;
> ....
>     {
>         // Create a filter, do some processing (i.e. reading a file)
>         Filter filter;
> 
>         ... do some processing
> 
>         // Get the output of the filter (a regular pointer to an image) 
> and assign it to the smart pointer
>         ptr = filter->GetOutput();
>     }
> 
> // Filter object now out of scope and so it is destroyed
> 
> 1. When I assign the output of the filter to the smart pointer, is the 
> reference count of the filter's output incremented to 2?
> 
> 2. Once the filter is out of scope (i.e. the braces), is the filter's 
> output image retained by the smart pointer (meaning the reference count 
> is now 1) ?
> 
> 
> 
> Thank you for your time.
> 
> Jayme
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
> 





More information about the Insight-users mailing list