[Insight-users] Releasing a FileReader releases the PixelContainer of already-read images?

Karthik Krishnan Karthik.Krishnan at kitware.com
Mon Jan 16 02:02:36 EST 2006


On Sun, 2006-01-15 at 20:26 -0800, Zachary Pincus wrote:
> Hello friends,
> 
> I've run into a troublesome issue while working on these new ITK  
> wrappers.
> 
> The problem is thus: say that I create a FileReader object, pass it a  
> valid file name, Update() it, and get the output image. Now, if the  
> FileReader is deallocated, it seems that the PixelContainer for the  
> output image is also deallocated, leaving me with an invalid image.
> 
> Is this default ITK behavior? If so, how can that behavior be turned  
> off? In general (not just in the ImageFileReader/ImageSource case)  
> does deallocating ITK filters also deallocate the PixelContainers of  
> the images that they generate?
> 
> If this is not default behavior, I guess I have some bug-hunting to  
> do in the wrappers.
> 
> Zach
> 
> PS. Here's an example from python:
>  >>> import itk
>  >>> infile='/Users/zpincus/Pictures/hamster.jpg'
>  >>> reader=itk.ImageFileReader[itk.Image[itk.US,2]].New 
> (FileName=infile)
>  >>> reader.Update()
>  >>> image=reader.GetOutput()
>  >>> image.GetPixelContainer() != None #test for null pixel container
> True
>  >>> del(reader)
>  >>> image.GetPixelContainer() != None
> False
> 
> Thus we can see that when I deallocate the reader, something happens  
> to deallocate the pixel container.

Replace  
  image = reader.GetOutput()
with
  image = itkImageUS2_Pointer(reader.GetOutput())

If image was a smart pointer, this wouldn't happen cause the reference
count will prevent it from disappearing when reader is deleted.

By default the type of the return type, "image" is going to be whatever
GetOutput() returns, which turns out to be a normal pointer, not a smart
pointer. 

-karthik

PS: If your intention of deleting objects is to save memory, you might
want to use the ReleaseDataFlagOn() method. Not that your case is any
different, from what ReleaseDataFlag would achieve, but it saves the
headache of remembering to delete objects.

> _______________________________________________
> 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