[Insight-users] Seg. fault with ReleaseDataFlag and JoinImageFilter

Luis Ibanez luis.ibanez at kitware.com
Fri, 30 Jan 2004 19:22:07 -0500


Hi Benjamin,

This looks like a bug...

It has been entered as Bug# 560
http://www.itk.org/Bug/bug.php?op=show&bugid=560&pos=0

-----

The worst thing that should happen by using
the ReleaseDataFlagOn() in a particular order
is that the reader would be forced to reallocate
an output image and to read the data from the
file again.


Independently of the fact that it is crashing,
you probably don't get any advantage in invoking
the Release data flag in this configuration of
the pipeline.


If you have strong memory concerns, a better way to
go may be to progressively destroy the filters that
are no longer needed.  A simple way to do this is to
use "{"  "}" and let the compiler destroy the filters
when they go out of scope.



Regards,


    Luis



-----------------------
Benjamin King wrote:

> Hello,
> 
> the attached code implements the following pipeline:
> 
> reader-->gradient magnitude-->JoinImageFilter
> |                                   ^
> |                                   |
> |-----------------------------------|
> 
> When I use reader->ReleaseDataFlagOn(), the program produces a 
> segmentation fault on joiner->Update() _unless_ preceeded by a 
> gradient->Update(). Please see the comments in the code. I use a fairly 
> recent ITK checkout and Linux.
> 
> My questions are:
> 
> 1) Why does this happen? Maybe during Update(), the gradient magnitude 
> filter makes the erroneous assumption that the reader's output is still 
> valid...
> 
> 2) Is there a better way to reduce the memory footprint than using 
> ReleaseDataFlagOn()?
> 
> 
> Thanks for the help,
>   Benjamin
> 
> 
> #include <iostream>
> #include <stdexcept>
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkGradientMagnitudeImageFilter.h"
> #include "itkJoinImageFilter.h"
> 
> using std::exception;
> 
> int main(int argc, char *argv[])
> {
>   if (argc != 2)
>   {
>     std::cout <<
>       "Makes a two component image of scalar value vs. gradient 
> magnitude\n"
>       "\n"
>       "USAGE: " << argv[0] << " <volume>\n"
>       "  <volume>: ITK volume file, e.g. Analyze. Example: foo.hdr\n"
>       << std::endl;
>     return 1;
>   }
> 
>   typedef itk::Image<unsigned short, 3> InputImageType;
>   typedef itk::Image<float, 3> DerivativeImageType;
> 
>   typedef itk::ImageFileReader<InputImageType> ImageReaderType;
>   ImageReaderType::Pointer reader = ImageReaderType::New();
>   reader->SetFileName(argv[1]);
>   reader->ReleaseDataFlagOn(); // Either insert comment to prevent seg.  
> fault...
> 
>   typedef itk::GradientMagnitudeImageFilter<InputImageType, 
> DerivativeImageType> GradientFilterType;
>   GradientFilterType::Pointer gradient = GradientFilterType::New();
>   gradient->ReleaseDataFlagOn();
>   gradient->SetInput(reader->GetOutput());
> 
>   typedef itk::JoinImageFilter<InputImageType, DerivativeImageType> 
> JoinFilterType;
>   JoinFilterType::Pointer combined = JoinFilterType::New();
>   combined->SetInput1(reader->GetOutput());
>   combined->SetInput2(gradient->GetOutput());
> 
>   try
>   {
>     // gradient->Update();  // ...or remove this comment to prevent seg. 
> fault
>     combined->Update();
>   }
>   catch (itk::ExceptionObject & e)
>   {
>     std::cerr << "ITK exception: " << e << std::endl;
>   }
>   catch (std::exception & e)
>   {
>     std::cerr << "std exception: " << e.what() << std::endl;
>   }
> 
>   return 0;
> }
>