[Insight-users] ImportImageFilter->ItkImage ...

Julien Jomier jjomier at cs.unc.edu
Tue Dec 21 10:27:58 EST 2004


Hi Lagaffe,

Who said C++ was easy? ;)

To understand when you should return a raw pointer (OutputTypeImage *) 
or a smart pointer (::Pointer), you might need to know a little bit more 
about smart pointers.

Basically, a smart is useful (user side point of view) because it 
deletes itself when you don't need it anymore (i.e the pointer is not 
assigned to any variable). As a developer, you need to make sure that 
the smart pointer is not deleted unintentionally.
When you assign a smart pointer to another smart pointer its reference 
count is increased automatically, and when the smart pointer is out of 
scope the reference count is decreased. When the reference count is zero 
the smart pointer deletes itself and therefore is allocated memory.

The problem you had in your function was that you created a smart 
pointer and assign it to a normal pointer (return 
myFilter->GetOutput()), this is a very bad idea because the smart 
pointer is not assigned and therefore is automatically deleted. Note 
that the inverse: assign a raw pointer to a smart pointer is good.
On the other hand, if you assign the 'myFilter->GetOutput()' to a smart 
pointer (the solution you had) the reference count is increased and the 
pointer is not deleted and you can use it outside your function.

In short, when you create and allocate (Type::Pointer myPointer = 
Type::New()) a smart pointer in a function and you return the local 
variable it's better to return a smart pointer.

hope that helps,

Julien

Mr Gaffe wrote:
> Thanks again julien, the definition is quite simple and obvious I 
> understand why I spend all the last few years with JAVA ;-)
>  
> I look in the ImageToImageFilter to understand the strategy used by ITK 
> to return an image after processing. The return type is an 
> OutputTypeImage and not a Pointer, so I confuse because I don't know 
> when and how to return an ImageType and when I have to return a Pointer 
> (as I did in myFunction) ?
>  
> Lagaffe
> 
> 
> */Julien Jomier <jjomier at cs.unc.edu>/* wrote:
> 
>     Hi Lagaffe,
> 
>     I assume your function is inside the class MyObject.
> 
>     In your .h:
> 
>     typedef TImage ImageType;
>     typedef typename ImageType::Pointer InputImagePointer;
>     static InputImagePointer myFunction(...);
> 
>     In your .txx:
> 
>     template
>     MyObject::InputImagePointer
>     MyObject
>     ::myFunction(...)
>     {
>     ....
>     }
> 
>     hope that helps,
> 
>     Julien
> 
>     Mr Gaffe wrote:
>      > Thanks jJulien it works,
>      >
>      > maybe if I can another question about the implementation of static
>      > methods in ITK...
>      >
>      > myFunction is a static function and it is define in a .h and
>     implemented
>      > in a .txx
>      > so if I put everything in my .h like you said it works:
>      >
>      > typedef typename ImageType::Pointer InputImagePointer;
>      > static InputImagePointer::Pointer myFunction(...)
>     &! gt; {
>      > ....
>      > }
>      > or if I don't want to use the typedef
>      > static typename TImage::Pointer myFunction(...)
>      > {
>      > ....
>      > }
>      > and you guess that My Object is templated using TImage ...
>      >
>      > BUT, I don't know how to implement it in my .txx and just define the
>      > header function in my .h as we do usually (it never compile ....)
>      >
>      > .h
>      > typedef typename ImageType::Pointer InputImagePointer;
>      > static InputImagePointer::Pointer myFunction(...);
>      > .txx
>      > static InputImagePointer::Pointer myFunction(...)
>      > {
>      > }
>      >
>      > Thanks julien for help,
>      > Lagaffe
>      >
>      > */Julien Jomier /* wrote:
>      >
>      > Hi Lagaffe,
>      >
>      > This is an issue with smart pointers.
>      > As you probably know smart pointers delete themselves when there are
>      > not
>      > assigned anymore.
>      > In your case, in myFunction(), the Impor! tFilter is creating a smart
>      > pointer as output image, but myFunction() is returning a raw pointer,
>      > which means that the reference count is not incremented and the
>     output
>      > pointer is destroyed at the end of the myFunction() scope.
>      > An easy fix is to make your function return a smart pointer and
>      > assign a
>      > smart pointer in your main.
>      >
>      > Something like:
>      >
>      > ImageType::Pointer myFunction()
>      > {
>      > ...
>      > return ImportFilter->GetOuput()
>      > }
>      >
>      > and in your program:
>      >
>      > typedef int PixelType;
>      > const int Dimension=2, Lx=7, Ly=5;
>      > typedef itk::Image < PixelType, Dimension > ImageType;
>      > ImageType::Pointer inputImage=myFunction(...);
>      >
>      > Let us k! now if that was the problem,
>      >
>      > Julien
>      >
>      > Mr Gaffe wrote:
>      > > hello,
>      > >
>      > > I try to build an itkImage with a function wich use an
>     ! > > importImageFilter; I can display my image inside my function
>     (so the
>      > > importation pipeline is ok), but when I try to do the same thing
>      > in my
>      > > program using the return itkImage pointer it doesn't work !
>      > >
>      > > basically this is my code;
>      > > function:
>      > >
>      > > ImageType* myFunction(...)
>      > > ... build the image and Import it with itkImportImageFilter
>      > > ... I did an Update
>      > > I can display my Image using std::cout << ImportFilter->GetOuput()
>      > > <
>      > >
>      > > finally,I return the pointer:
>      > > return ImportFilter->GetOuput()
>      > >
>      > > In my program:
>      > > typedef int PixelType;
>      > > const int Dimension=2, Lx=7, Ly=5;
>      > > typedef itk::Image < PixelType, Dimensi! on > ImageType;
>      > > ImageType* inputImage=myFunction(...);
>      > >
>      > > When I try to display std:cout<
>      > <<>> pointer value and not all the usual image information .... and
>      > if I try
>      > > to use inputImage to do something it crash !
>      > >
>      > > I think, I am not using the good return type or something like
>     that?
>      > >
>      > > thanks for help
>      > > lagaffe
>      > >
>      > >
>      > >
>      > >
>      > >
>      >
>     ------------------------------------------------------------------------
>      > > Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage
>      > pour vos
>      > > mails !
>      > > Créez votre Yahoo! Mail
>      > >
>      > >
>      > >
>      > >
>      > >
>      >
>     ------------------------------------------------------------------------
>      > >
>      > > _______________________________________________
>      > > Insight-users mailing list
>      > > Insight-users at itk.org
>      > > http://www.itk.org/mailman/listinfo/insight-users!
>      >
>      >
>     ------------------------------------------------------------------------
>      > Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage
>     pour vos
>      > mails !
>      > Créez votre Yahoo! Mail
>      >
>      >
>      >
>      >
>      >
>     ------------------------------------------------------------------------
>      >
>      > _______________________________________________
>      > Insight-users mailing list
>      > Insight-users at itk.org
>      > http://www.itk.org/mailman/listinfo/insight-users
> 
> ------------------------------------------------------------------------
> Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos 
> mails !
> Créez votre Yahoo! Mail 
> <http://fr.rd.yahoo.com/mail/taglines/*http://fr.rd.yahoo.com/evt=25917/*http://fr.rd.yahoo.com/mail_fr/mail_campaigns/splash/taglines_250/default/*http://fr.promotions.yahoo.com/mail/creer28.html> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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