[Insight-users] Failed to allocate memory for image

Luis Ibanez luis.ibanez at kitware.com
Mon Sep 3 19:11:45 EDT 2007


Hi Ashish,


Thanks for letting us know of your findings,


As you see,
it usually pays off to cut the problem into smaller pieces.


---

I'm looking at your code, but I'm failing to grasp the
the purpose of the "readfiles()" method.


What I see is that for every DICOM image you are taking
its MetaDataDictionary, locating its DICOM tag 0010|0020
which corresponds to Patients ID,
and then...

             Don't do anything with it.


Also your method for finding the Patient Name tag is quite
convoluted. There are ways to do this in a simpler manner.

Please look at the ITK Software Guide


    http://www.itk.org/ItkSoftwareGuide.pdf


Section 7.12.5, "Printing Dicom Tags from One Slice",
in pdf-page: 337-343.

The source code is in the directory:


       Insight/Examples/IO/
          DicomImageReadPrintTags.cxx
          DicomPrintPatientInformation.cxx


In particular, note that std::string has a comparator
operator, you don't need to do a comparison character
by character...


----

In any case, I fail to see why the invocation of readlines()
will result in a memory leak.

You may have to first simplify that method by following
the examples suggested above, and then, you may want to
turn on/off sections of the code in order to detect the
exact place where the memory leak (if any) takes place.

BTW What is your platform ?

If you are doing this on Linux, you could use Valgrind
to detect potential memory leaks.



      Regards,


         Luis



----------------------
Ashish Singh wrote:
> 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 
> <mailto: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
>     <mailto: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 <mailto:Insight-users at itk.org>
>>  http://www.itk.org/mailman/listinfo/insight-users
> 
> 
> 


More information about the Insight-users mailing list