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

**URL:** https://discourse.itk.org/t/is-it-safe-to-store-itk-image-inside-itk-dataobject/1546
**Category:** Engineering
**Tags:** memory-managment
**Created:** [January 25, 2019, 9:39am UTC](https://discourse.itk.org/t/is-it-safe-to-store-itk-image-inside-itk-dataobject/1546 "2019-01-25T09:39:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Athius](https://discourse.itk.org/user_avatar/discourse.itk.org/athius/32/304_2.png) [@Athius](https://discourse.itk.org/u/Athius)
#### Post date: [January 25, 2019, 9:39am UTC](https://discourse.itk.org/t/is-it-safe-to-store-itk-image-inside-itk-dataobject/1546/1 "2019-01-25T09:39:02Z")

</div>

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](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

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [January 25, 2019, 2:15pm UTC](https://discourse.itk.org/t/is-it-safe-to-store-itk-image-inside-itk-dataobject/1546/2 "2019-01-25T14:15:43Z")

</div>

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`](https://itk.org/Doxygen/html/classitk_1_1ImageBase.html) 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

---

<div class="post-metadata">

### Author: ![Athius](https://discourse.itk.org/user_avatar/discourse.itk.org/athius/32/304_2.png) [@Athius](https://discourse.itk.org/u/Athius)
#### Post date: [January 25, 2019, 2:18pm UTC](https://discourse.itk.org/t/is-it-safe-to-store-itk-image-inside-itk-dataobject/1546/3 "2019-01-25T14:18:48Z")

</div>

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](mailto:noreply@discourse.itk.org) a écrit :
