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

Luis Ibanez luis.ibanez at kitware.com
Tue Oct 13 09:38:31 EDT 2009


Hi Michael,

We will have to see the code of "other stuff" in the
 getAnything() method...

or,

If you can run this in Debug mode and look at the
call stack after the program crashes, that will give
us a hint of what is going wrong.



The typical reason for crashing in "free" is to attempt
to deallocate a pointer that has already been deallocated.

All that points to the itkImportImageFilter and its boolean
flag as the prime suspect....

Are you using the itkImageImageFitler ?


    Thanks


          Luis


--------------------------------------------------
On Tue, Oct 13, 2009 at 5:01 AM, Michael Xanadu
<xanadu.michael at googlemail.com> wrote:
> Hi Luis,
>
> thanks for your response. It helped me a lot. Actually my problem faces case
> (2). I decided to return a SmartPointer without const. So I changed the code
> to:
>
>     template <class TImage>
>     ImageType::Pointer FilterManager<TImage>::getAnything(int anyParameter)
>     {
>         // create filter and other stuff
>         return anyItkFilter->GetOutput();
>     }
>
> In this case I get a handful error messages when compiling. It seems that I
> have to change the code again:
>
>     template <class TImage>
>     typename FilterManager<TImage>::ImageType::Pointer
> FilterManager<TImage>::getAnything(int anyParameter)
>     {
>         // create filter and other stuff
>         return anyItkFilter->GetOutput();
>     }
>
> In this case the compiler works fine. Now I want to use that method so I
> did:
>
>     ImageType::Pointer myImage = filterManager->getAnything(param);
>
> But once again the applications crashes at this point in "free.c". Do you
> know what went wrong? Is the declaration ok?
>
> Regards, Michael
>
>
>
>
> 2009/10/13 Luis Ibanez <luis.ibanez at kitware.com>
>>
>> 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
>
>


More information about the Insight-developers mailing list