Deleting or Over-writing Files Which have been Read using ReadFile

Image files which have been read by DicomObjects using ReadFile cannot normally be deleted (or over-written using WriteFile etc.) until the file is released. This is because memory mapping is enabled (Default DicomObjects's behaviour), causing only the data actually needed at any time to be read into memory. This requires that the file be locked whilst the associated image object exists.

You can use one of the following three methods to solve this problem:

Registry Method

Create/Set the "DisableMemoryMapping" registry value to "1". This can be done in two ways:

Using the real registry
HKEY_LOCAL_MACHINE/Software/Medical Connections/DicomObjects/DisableMemoryMapping = 1 (it’s a DWORD)
Using the internal pseudo-registry
- e.g. in VB
Dim g as New DicomGlobal
g.RegWord("DisableMemoryMapping") = 1

 

Use "DicomImage.DecompressAll" method

DicomObjects is trying not to read everything into memory (for efficiency and performance purposes) unless it's necessary or it's told to do so (by calling DecompressAll or setting the above registry entry). Once everything has been read into memory the file mapping will be disabled and the file is unlocked. The downside of this method is of course that the pixel data is fully decompressed, so this is not a good idea if you wish to write the data back out again in the original compressed form, in which case the above registry method would be better.

There is also a file deletion problem specific to Delphi programming IDE, click Deleting_Files_In_Delphi for more details.

  • In DicomObjects.NET, use the DicomImage Dispose method before attempting to delete an image file that has been read into a DicomImage object. With this approach one can delete the file yet still benefit from the advantages of memory mapping.
  • When using the COM version of DicomObjects from a .NET environment, it is important to realise that the COM object will not be released (and therefore the lock on the file not released) until the .NET garbage collection system has completed a run. To ensure that this happens, you will need the following code
[set all objects referencing the image to null]
GC.Collect();
GC.WaitForPendingFinalizers();
[delete the file]

 

CloseFilesAfterReading property

In .NET version of Dicom Objects there is a 3rd method to achieve the file release. You can set

DicomGlobal.CloseFilesAfterReading = true  

this will force the whole file into memory and released the file on the disk.

Relevance: