[Insight-developers] Lack of type safety in ImageSource

Bradley Lowekamp blowekamp at mail.nih.gov
Tue Mar 3 09:39:36 EST 2009


Hello,

As I was looking at BayesianClassifierImageFilter, which is a filter  
which has multiple outputs of different types. I was looking at  
ImageSource and noticed some problems when the filters have different  
outputs. As this lack of type safety can cause segfaults and pointers  
to the wrong type I think they are bugs.

The problem is with the following two methods:

typename ImageSource<TOutputImage>::OutputImageType *
ImageSource<TOutputImage>
::GetOutput(unsigned int idx)
{
   return static_cast<TOutputImage*>
     (this->ProcessObject::GetOutput(idx));
}


ImageSource<TOutputImage>
::AllocateOutputs()
{
   OutputImagePointer outputPtr;

   // Allocate the output memory
   for (unsigned int i=0; i < this->GetNumberOfOutputs(); i++)
     {
     outputPtr = this->GetOutput(i);
     outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
     outputPtr->Allocate();
     }
}

GraphNthOutput has a minor issue too.

I see a real problem with this implementation of AllocateOutputs. In  
that outputPtr may not be of the type we have a pointer to. In the  
case of BayesianClassifierImageFilter using this parent method causes  
a segfault. I see two solutions two this based on what is done in  
ImageSource and ImageToImageFilter. Perform a dynamic_cast and do  
nothing if the cast fails. Or we could case to ImageBase, but that is  
currently problematic since Allocate, is not a virtual method in  
ImageBase.

In GetOutput, a dynamic_cast could be used as well. I ran a couple of  
experimentals on choptank.nlm yesterday with and with out a  
dynamic_cast there:
http://www.cdash.org/CDash/buildSummary.php?buildid=282934
http://www.cdash.org/CDash/buildSummary.php?buildid=282954

 From the dash board there appears no significant performance  
difference. But there are 4 new tests that are failing. I only looked  
at itkContourDirectedMeanDistanceImageFilterTest, and there is clearly  
a problem in it's Allocate method. I would think that there are bugs  
in the other classes as well.

========================================================
Bradley Lowekamp
Lockheed Martin Contractor for
Office of High Performance Computing and Communications
National Library of Medicine
blowekamp at mail.nih.gov


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20090303/cc33ccb9/attachment.htm>


More information about the Insight-developers mailing list