DicomImage Object destruction

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
todor_petkov
Posts: 5
Joined: Wed, 2005-01-26, 09:25

DicomImage Object destruction

#1 Post by todor_petkov »

Hello, I have a question about memory management in DCMTK.
In the code that follows I am trying to load a series of dicom images from
a directory and store them in a single 3d array...

Code: Select all

/********************/
Here is var declaration:

DicomImage     *di;
DcmFileFormat  dfile;
DcmDataset      *dataset;

/******************/
Here is pseudo code

for number of images in directory
{
   dfile.loadFile (file_name);
   dataset = dfile.getDataset ();
 
   xfer = dataset->getOriginalXfer ();
   mode = CIF_MayDetachPixelData | CIF_TakeOverExternalDataset;

   di = new DicomImage (&dfile, xfer, mode);
   di->getOutputData (....);

   di->deleteOutputData ();       
   
   delete di ;      <---------!!!!!!!!!!!!!!!!!!-------------- segmentation fault.

}
/**************************************************/
So my question is how to free DcmDataset and DicomImage pointers,
which should be freed first, which is dependent on which?

I have profiled this piece of code ( without deleting di ) using rational
purify and I get memory leaks if i do not use delete di. If I use "delete di" I get
segmentation faults.

Unfortunately DCMTK example code shows only how to do memory allocation in a stand alone program in which all memory is freed and returned to OS after program exit... What about library wrap code using DCMTK?!

Thanks a lot,

Todor

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#2 Post by Jörg Riesmeier »

The CIF_TakeOverExternalDataset flag tells the DicomImage object to delete the DICOM dataset/fileformat on destruction. However, "dfile" is a local variable which is created on the stack and will be destroyed automatically. So, in your case just omit the CIF_TakeOverExternalDataset flag ...

The DcmDataset is destroyed automatically in the context of DcmFileFormat. Btw, the line "di->deleteOutputData ();" is also not required when you delete the DicomImage object in the next line anyway.

And finally, there are some examples in the toolkit that show how to use the DicomImage class inside a library. Just search for the word "DicomImage".

todor_petkov
Posts: 5
Joined: Wed, 2005-01-26, 09:25

#3 Post by todor_petkov »

Ok, it worked!!!!!!!!
Thanks a lot...

Todor

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest