Is it safe to store itk::Image inside itk::DataObject

Hello everyone,

I have some question regarding the memory management.

I want to separate my application in multiple modules and manage all itk::Image PixelType available.
To store an itk::Image inside a itk::DataObject with the Pixel type and the Image Dimension then I do some cast to get back my image when I call a filter.

This is an example of what I currently do for my filters:

template<typename Image>
void doFilter(const itk::DataObject::Pointer & input, itk::DataObject::Pointer & output)
{
      using Filter = itk::FooFilter<Image>;
      typename Filter::Pointer filter = Filter::New();
      filter->SetInput(static_cast<Image*>(input.GetPointer());
      filter->Update();
      output = filter->GetOutput();
}

I have some memory issues (empty images, segmentation fault…), so I suspect that I don’t understand quite well the memory management of itk smart pointer. I read the documentation (https://itk.org/ItkSoftwareGuide.pdf section 3.2.3 and 3.2.4) but I don’t find anything on copy of itk smart pointer to a parent class smart pointer.

Can anyone tell me what I do wrong or not please?

Thank you.

Romain

Hello Romain,

Yes, in general it is safe to store the Image in a DataObject SmartPointer. If it is known to be an Image, ImageBase may be more appropriate.

To check whether the logic is correct regarding Image * and the type of input.GetPointer(), use a dynamic_cast instead of static_cast and check that the result is not nullptr.

Hope this helps,
Matt

Hello Matt,

Thank you for your quick answer!

I did static_cast for performance issue but you’re correct it’ll be better to do some check on the cast result.

Thank you,

Romain

Le ven. 25 janv. 2019 Ă  15:16, Matt McCormick via ITK noreply@discourse.itk.org a Ă©crit :

1 Like