Explicitly dispose resources such as DicomFile and DicomImage

724 views
Skip to first unread message

Vitaly Pavluk

unread,
Oct 17, 2012, 10:39:30 AM10/17/12
to fo-d...@googlegroups.com
Hi.

Does fo-dicom offer the API which can help me explicitly dispose the resources allocated by DicomFile and DicomImage class instances?
My case is the following:
var dicomFile = DicomFile.Open("C:\folder\dicomFile.dcm");
//some file processing code
dicomFile.Close(); 
// I have not found the Close or Dispose methods which will release all resources related to the dicomFile object and releases OS handle and internal streams related with file system object "C:\folder\dicomFile.dcm"

Should I worry about such things or not ?
----
Thank you in advance.

Colby Dillion

unread,
Oct 17, 2012, 2:45:18 PM10/17/12
to fo-d...@googlegroups.com
There is no need to dispose the object as it does not hold open any handles. After initially reading the file, the library closes the input stream and will reopen it as needed.

Colby

Vitaly Pavluk

unread,
Oct 19, 2012, 7:20:43 AM10/19/12
to fo-d...@googlegroups.com

Hi,
I briefly looked through the source code of fo-dicom project and have found the following:
Class FileByteSource has  the private BinaryReader _reader; private Stream _stream; fields which allocates unmanaged resources and supports IDisposable interface for deterministic resource releasing but I have not found any code where FileByteSource class releases these resources via using statement or explicitly closing the streams/readers. 
I have not dug deeper into the  codebase but just unreleased streams leads  to this error in external application
class Program {
     
public static void Main () {
                 
var dicomFile = DicomFile.Open("c:\filename.dcm");
                  var creationDate = dicomFile.Dataset.Get<DateTime>(DicomTag.CreationDate);

/*here on File.Move I continuously get IOException 'The process cannot access the file because it is being used by another process.'*/                  
/* Uncomment these code to allow the pure .NET hack to get rid of exception
                         GC.Collect();
                         GC.WaitForPendingFinalizers(); Finalize the opened streams and releases the underlying unmanaged resources
*/
                 
File.Move(@"c:\filename.dcm","c:\temp\filename.dcm")                   
}}


Could you please give some guidelines how to use objects of DicomFile class correctly to avoid locking the dcm file which is opened 
and /or give a hint to API in "FO-Dicom" library to release the resource allocated by DicomFile in deterministic way.

Thank you in advance.
PS: This library is the most advanced and powerful DICOM library which I have ever used. 
Thank you for amazing project.

Colby Dillion

unread,
Oct 19, 2012, 11:17:34 AM10/19/12
to fo-d...@googlegroups.com
The intended behavior of the DicomFile open and save methods was for the class to release the resources but retain a reference to the file in case we needed to load more data. I've corrected the code to ensure that the intended behavior is the actual behavior. I've tested your code and was able to reproduce the error and it appears to be fixed with these changes.

Also added is the ability to move the file referenced by a FileReference. This is preferable to File.Move if you intend to continue using the DICOM file after moving.
var dicomFile = DicomFile.Open("c:\filename.dcm");
dicomFile.File.Move("destination.dcm", true);

You can pull the latest code with these changes from GitHub, or I'm sure there will be another release in the next few days if you prefer binaries.

Colby

Vitaly Pavluk

unread,
Oct 19, 2012, 11:39:00 AM10/19/12
to fo-d...@googlegroups.com
Thank you ! 
It works!!
Reply all
Reply to author
Forward
0 new messages