This is a Delphi specific problem (Believed to be solved in Version 7 and afterwards). Getting errors when trying to delete an image file, even when the Images collection has already been cleared. See the following code:
Images.ReadFile('C:\TestImage.dcm'); Images.Clear; if not DeleteFile('C:\TestImage.dcm') then ShowMessage('couldn't delete TestImage.dcm');
Using the above code, developers always hitting the red line. This is actually caused by the first line where DicomImages.ReadFile is returning a DicomImage object, which is passed (By Delphi) to a hidden variable. This hidden variable does not get released until the end of current procedure (or function) and DeleteFile won’t be able to get access to the image file locked by that hidden DicomImage variable, as described in Cannot Delete Files.
You can explicitly pass ReadFile’s return value to a DicomImage variable and set it to nil immediately after ReadFile:
var myNullImage : DicomImage; ... myNullImage := Images.ReadFile('C:\TestImage.dcm'); myNullImage := nil; Images.Clear; if not DeleteFile('C:\TestImage.dcm') then ShowMessage('couldn't delete TestImage.dcm');
The Image file is unlocked and can be deleted now.
Another file deletion problem (caused by memory mapping) has been reported and documented in Deleting-Or-Over-Writing-Files-Which-Have-Been-Read.