[Insight-users] Failed to allocate memory for image

Ashish Singh mrasingh at gmail.com
Tue Aug 28 13:14:33 EDT 2007


Hi Luis,

I ran the code suppressing header processing part and I don't get the
errors. So we now know that the problem is in that part. Here is the code
with that part:

--------
const unsigned int InputDimension =3;
typedef unsigned char InputPixelType;
typedef itk::Image< InputPixelType, InputDimension > InputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;
typedef itk::GDCMImageIO ImageIOType;
typedef itk::MetaDataDictionary   DictionaryType;

unsigned long fni;//in class header file
InputImageType::Pointer image;//in class header file
DictionaryType dictionary;//in class header file

image = InputImageType::New();//in class constructor

for(fni=0;fni<250000;fni++)
{

    ReaderType::Pointer reader2 = ReaderType::New();

    ImageIOType::Pointer gdcmImageIO2 = ImageIOType::New();

    [set filename for reader2]
    //fname = this->directory->absoluteFilePath(this->files[fni]);
    //reader2->SetFileName(std::string( fname.toAscii()));

    reader2->SetImageIO(gdcmImageIO2);
    try
    {
        reader2->Update();
    }
    catch (itk::ExceptionObject & e)
    {
        std::cerr << "exception in file reader " << std::endl;
        std::cerr << e << std::endl;
        return EXIT_FAILURE;
    }

    [ code for processing information from header of each image from here]

    this->image=reader2->GetOutput();
    this->dictionary=this->image->GetMetaDataDictionary();
    this->readfiles();
    [ code for processing information from header of each image till here]

    //reader2->Delete();//not implemented
    //gdcmImageIO2->Delete();//not implemented

}

readfiles()
{
   int flag;
   string pid,pidtag;
   pidtag="0010|0020";
   DictionaryType::ConstIterator itr = this->dictionary.Begin();
   DictionaryType::ConstIterator end = this->dictionary.End();
   while( itr != end )
   {
     itk::MetaDataObjectBase::Pointer  entry = itr->second;
     MetaDataStringType::Pointer entryvalue =
dynamic_cast<MetaDataStringType *>( entry.GetPointer() ) ;
     if( entryvalue )
     {
       std::string tagkey   = itr->first;
       std::string labelId;
       bool found =  itk::GDCMImageIO::GetLabelFromTag( tagkey, labelId );
       std::string tagvalue = entryvalue->GetMetaDataObjectValue();
       flag=0;

       //get patient Id in a string
       for(i=0;i<9;i++)
       {
          if(tagkey[i]==pidtag[i])
          flag++;
       }
       if(flag==9)
       {
          pid=tagvalue;
          flag=0;
       }
       else
         flag=0;
     }//if ends here
   itr++;
  }//while ends here
}
----------

I would really appreciate if you could guide me as to what is going wrong
here. Where is that memory leak and how do I fix it?
Please help.

Thanks,
Ashish

On 8/27/07, Ashish Singh <mrasingh at gmail.com> wrote:
>
> Hi Luis,
>
> Thanks for replying.I am using ITK- 3.2.0.
>
> How do I modify this code without having to create reader and GDCMImageIO
> at every iteration? Maybe that is what is creating problem here.
> I am going to run the application with the' header processing part'
> suppressed and will let you know exactly what happens. In the meantime, if
> you can tell me the modifications for using single reader and GDCMImageIO
> for all images, I could try that out too.
>
> Thanks,
> Ashish
>
> On 8/27/07, Luis Ibanez <luis.ibanez at kitware.com> wrote:
> >
> >
> > Hi Ashish,
> >
> > What version of ITK are you using ?
> >
> >    This looks like a memory leak...
> >
> > Note that you really don't have to create
> > the reader an the GDCMImage IO at every
> > iteration of the loop. That being said,
> > your code is still a valid construction.
> >
> > Do you get the same error if you suppress
> > the code that is "oprocessing information from
> > header of each image" ?
> >
> > It may be that that section of code contains
> > a memory leak...
> >
> >
> >    Please let us know
> >
> >
> >       Thanks
> >
> >
> >           Luis
> >
> >
> > --------------------
> > Ashish Singh wrote:
> > > Hi,
> > >
> > > I have written an application to read and process dicom header
> > > information. My application runs fine for small number of images but
> > > gives an error when it reaches about 70,000 images(the number varies
> > > each time).
> > > The error that I get is the following:
> > > ---------------
> > > itk::ExceptionObject <01C5AC80>
> > > Location: "unsigned char *_thiscall itk::ImportImageContainer<unsigned
> > > long,unsigned char>::AllocateElements<unsigned long> const"
> > > File:
> > > c:\myinstalls\insighttoolkit-
> > 3.2.0\code\common\itkImportImageContainer.txx
> > >
> > > Line: 188
> > > Description: Failed to allocate memory for image.
> > > ---------------
> > >
> > > The images don't belong to a single series.The application is running
> > on
> > > windows XP Pro x64 platform. The relevant portion of the code looks
> > like
> > > this -
> > > --------
> > > const unsigned int InputDimension =3;
> > > typedef unsigned char InputPixelType;
> > > typedef itk::Image< InputPixelType, InputDimension > InputImageType;
> > > typedef itk::ImageFileReader< InputImageType > ReaderType;
> > > typedef itk::GDCMImageIO ImageIOType;
> > >
> > > unsigned long fni;
> > > for(fni=0;fni<250000;fni++)
> > > {
> > >
> > >     ReaderType::Pointer reader2 = ReaderType::New();
> > >
> > >     ImageIOType::Pointer gdcmImageIO2 = ImageIOType::New();
> > >
> > >     [set filename for reader2]
> > >     //fname = this->directory->absoluteFilePath(this->files[fni]);
> > >     //reader2->SetFileName(std::string( fname.toAscii()));
> > >
> > >     reader2->SetImageIO(gdcmImageIO2);
> > >     try
> > >     {
> > >         reader2->Update();
> > >     }
> > >     catch (itk::ExceptionObject & e)
> > >     {
> > >         std::cerr << "exception in file reader " << std::endl;
> > >         std::cerr << e << std::endl;
> > >         return EXIT_FAILURE;
> > >     }
> > >
> > >     [ code for processing information from header of each image here]
> > >
> > >     //reader2->Delete();//not implemented
> > >     //gdcmImageIO2->Delete();//not implemented
> > >
> > > }
> > > ----------
> > > I cannot call a delete inside the 'for' loop. It gives me an error
> > > saying that an error has occured and do you want to send information
> > to
> > > Microsoft?
> > > I also did some reading in ITK software guide, where it says that a
> > > smart pointer takes care of delete by itself. I also tried Register(),
> > > UnRegister(), ReleaseDataFlagOn().None of these work.
> > > Can anyone please tell me what is going wrong here and how to fix it?
> > > How can I release the memory for the reader? Is there a simpler way to
> > > do this?
> > > I will really appreciate if someone can help me with this.
> > >
> > > Thanks,
> > > Ashish
> > >
> > >
> > >
> > ------------------------------------------------------------------------
> > >
> > > _______________________________________________
> > > Insight-users mailing list
> > > Insight-users at itk.org
> > > http://www.itk.org/mailman/listinfo/insight-users
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20070828/26bcb232/attachment.html


More information about the Insight-users mailing list