[Insight-developers] How to return an itk::Image correctly?

Luis Ibanez luis.ibanez at kitware.com
Mon Oct 12 18:05:29 EDT 2009


Hi Michel,

Thanks for the clear description of the problem that you are facing.


There are two separate issues here:


A) Whether the returned type should be "const" or not.

B) Whether the returned type should be a raw pointer
     or a Smart Pointer.



The reason why your application crashes is most
likely related to (B) than to (A).



Let me explain (B) first:

ITK Images are owned by the filter that generate them
as output. By "owned" we mean that the filter holds a
SmartPointer to the image and will probably be the
one responsible for destroying the image, when the
filter's destructor is called.

When creating functions that return ITK images,
two situations typically arise:

   1) The function is a method that belongs to a class,
        where the filters are member variables and
        therefore they scope will keep them alive even
        after the method in question has been executed.

or

    2) The function creates internally the filters that
         will produce an ITK image as output, and the
         scope of the filters will finish when the method
         finishes executing.

If you are in the case (1), then the function that
creates the image can safely return the image as a
raw pointer, since the filter that holds the smart pointer
to the image will be kept alive by the class of which
this filter is  a member variable.

If you are in case (2), the you MUST return the image
as a SmartPointer, to prevent the image from being
deleted when the filter is destroyed at the point of
getting out of the scope of the method.


Regarding (A):

Whether to use "const" pointers or non-const
pointers.

If the image that you return is still own by a filter,
(like in case (1)),  then you MUST use "const"
pointers, because the only class authorized to
change the content of the image is that filter.

If the image that you return, is no longer own
by a filter (such as in case (2)), then you don't
have to return the image as "const". In this
case is up to you to choose "const" or "non-const".



 Please let us know if you have further questions,


     Thanks


           Luis


--------------------------------------------------------------------
On Mon, Oct 12, 2009 at 11:08 AM, Michael Xanadu
<xanadu.michael at googlemail.com> wrote:
> Hi my friends,
>
> I have a class called FilterManager which uses templates and is derivated
> from ProcessObject. This class has a method getAnything() which shall return
> the pointer of an itk:Image. I looked at the example "VTKImageToImageFilter"
> and what they did was something like that:
>
>
>     template <class TImage>
>     const typename FilterManager<TImage>::ImageType *
> FilterManager<TImage>::getAnything(int anyParameter) const
>     {
>         // any code
>         return anyItkFilter->GetOutput();
>     }
>
> Now I want to call the method in this way:
>
>     typedef itk::GremlinDetector<Short3DType> GremlinDetectorType;
>     GremlinDetectorType::popUpImage(filterManager->getAnything(number));
>
> where "popUpImage" (a static method) is declared in this way:
>
>     template <class TImage>
>     void GremlinDetector<TImage>::popUpImage(ImageType *image) //ImageType
> is defined in the header as: typedef TImage ImageType;
>     {
>         //do anything with the image
>     }
>
>
> It seems that I have a problem with "const" in the method header. Of course
> the compiler complains:
>
>     error C2664: 'itk::GremlinDetector<TImage>::popUpAllImages' : cannot
> convert parameter 1 from 'const itk::Image<TPixel,VImageDimension> *' to
> 'itk::Image<TPixel,VImageDimension> *'
>
> But I don't know how to fix that problem. If I remove the "const"
> expressions from the getAnything()-header the application crashes and I
> can't make popUpImage() to a const method, because I may not change that
> code. Can somebody help me please?
>
>
> Regards, Michael
>
>
> _______________________________________________
> 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-developers
>
>


More information about the Insight-developers mailing list