Remove element from DcmPixelSequence list, but do not able to delete it

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
ashishbme
Posts: 30
Joined: Tue, 2011-05-10, 14:32

Remove element from DcmPixelSequence list, but do not able to delete it

#1 Post by ashishbme »

Hi,

I am trying to compressed byte sequence in DICOM file. this code is working (still i need to refine). i am inserting DcmPixelItem in DcmPixelSequence. after dicom image is saved, i need to clean up heap objects. i am removing from pixelsequence but when i am try to delete DcmPixelItem objects. it crashing while calling DcmList::deleteAllElements(). there is not list available because of i delete it . is there something i am not doing correct in this code or what is right way delete DcmPixelItem and DcmPixelData objects. Is tehre any sequence i need to follow while deleting.

Code: Select all

auto pixelSequence = new DcmPixelSequence(DcmTag(DCM_PixelData, EVR_OB));
		// add the table offset
		auto offsetTable = new DcmPixelItem(DcmTag(DCM_Item, EVR_OB));
		Status = pixelSequence->insert(offsetTable);
		if (Status.bad())
		{
			return false;
		}
		auto pixelItem = new DcmPixelItem(DcmTag(DCM_Item, EVR_OB));
		ptrdiff_t size;
		ReadAllBytes(m_pVideoFileName.c_str(), &size);
		if (m_pixels != NULL)
		{
			pixelItem->putUint8Array(OFreinterpret_cast(Uint8*, m_pixels), size);
			Status = pixelSequence->insert(pixelItem);
			if (Status.bad())
			{
				return false;
			}
		}
		else
		{
			return false;
		}
		
		auto pixelData = new DcmPixelData(DCM_PixelData);
		pixelData->putOriginalRepresentation(EXS_MPEG4HighProfileLevel4_1, NULL, pixelSequence);
		Status =  pDataSet->insert(pixelData, OFTrue);
		if (Status.good())
		{
			pFileFormat.saveFile(m_pDicomFileName.c_str(), EXS_MPEG4HighProfileLevel4_1, EET_UndefinedLength, EGL_recalcGL);
		}
		else
		{
			return false;
		}
		
		
		if (pixelSequence != NULL)
		{
			pixelSequence->remove(pixelItem);
			pixelSequence->remove(offsetTable);

		}
Thank you
Ashish

Michael Onken
DCMTK Developer
Posts: 2048
Joined: Fri, 2004-11-05, 13:47
Location: Oldenburg, Germany
Contact:

Re: Remove element from DcmPixelSequence list, but do not able to delete it

#2 Post by Michael Onken »

Hi,

my first impression is that your code deletes the pixel items but instead, DcmPixelSequence already has taken ownership for them.

Look at the the documentation of DcmPixelSequence::insert() which states that the class takes ownership of the provided pixel item. Thus you delete the pixel item and when the DICOM dataset with the DcmPixelSequence goes out of scope, it will try to free the memory a second time and your code crashes.

The same applies, by the way, for the DcmPixelData (pixelData) element, whose memory also belongs to the DICOM dataset (see DcmDataset/DcmItem documentation) after calling insert() for it.

As a summary: Remove both delete statements from your code and you're fine. You could also use a checker tool like valgrind on Linux to ensure that no memory leaks are present.

Best regards,
Michael

ashishbme
Posts: 30
Joined: Tue, 2011-05-10, 14:32

Re: Remove element from DcmPixelSequence list, but do not able to delete it

#3 Post by ashishbme »

Thank you Michael :) , for your inputs. That answers my question.

Regards
Ashish

Post Reply

Who is online

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