Files being locked, not released by PEReader

Topics: Metadata Model, PE reader
Feb 19, 2010 at 2:22 PM

Hi, I am having a bit of trouble with some files being locked and not released. As far as I can tell it is occouring in the PEReader/LoadUnitFrom/IModule handling. Below is a small code sample that illustrates the issue.

PeReader.DefaultHost host = new PeReader.DefaultHost();
IModule module = host.LoadUnitFrom(exeFile) as IModule;

try
{
    module = null;
    System.GC.Collect();

    System.IO.File.Delete(exeFile);
}
catch(Exception e)
{
    System.Diagnostics.Debug.WriteLine("failed: " + e.ToString());
}

So we read a module in from a file, then nullify the reference and run the collector (just to be sure everything is disposed of if it can be). Then when we try to delete the file "exeFile" an exception is thrown (permission denied...). On the other hand if we replace the line: "IModule module = host.LoadUnitFrom(exeFile) as IModule;" with "IModule module = null" then the file delete succeeds without any exceptions.

Mark

 

Coordinator
Sep 24, 2010 at 10:26 PM

The MetadataReaderHost object now implements IDisposable. If you call Dispose on the host object, all files opened by it will be closed and all umanaged memory allocated with it will be released. See Change Set 51451.