Files
ILSpy/ICSharpCode.Decompiler/Metadata/PEFile.cs
T
Siegfried PammerandClaude Opus 4.7 a8d841199d Lift IDisposable to MetadataFile and dispose evicted assemblies.
MetadataFile now declares IDisposable using the canonical pattern
(public non-virtual Dispose() + protected virtual Dispose(bool)).
PEFile and WebCilFile become sealed and override Dispose(bool) to
release the PEReader and MemoryMappedViewAccessor they own;
ResourcesFile is also sealed. PortableDebugInfoProvider disposes the
MetadataReaderProvider it owns. LoadedAssembly implements IDisposable
and disposes both the loaded MetadataFile and the debug-info provider.

AssemblyList.Unload / Clear / ReloadAssembly / HotReplaceAssembly now
dispose the LoadedAssembly instances they evict, fixing a resource leak
where every "Reload Assembly" held the previous PEReader (and the
underlying file handle / memory-mapped view) alive until GC eventually
finalized it.

The disposal contract terminates at the AssemblyList tier: downstream
holders of MetadataFile (MetadataModule, DecompilerTypeSystem,
AssemblyListSnapshot, ...) hold borrowed references rather than owned
ones, so making the base IDisposable does not cascade into CA1001 /
CA2213 warnings elsewhere.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-05-08 21:22:20 +02:00

108 lines
3.9 KiB
C#

// Copyright (c) 2018 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#nullable enable
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.Metadata
{
[DebuggerDisplay("{FileName}")]
public sealed class PEFile : MetadataFile, IModuleReference
{
public PEReader Reader { get; }
public PEFile(string fileName, PEStreamOptions streamOptions = PEStreamOptions.Default, MetadataReaderOptions metadataOptions = MetadataReaderOptions.Default, MetadataStringDecoder? utf8Decoder = null)
: this(fileName, new PEReader(new FileStream(fileName, FileMode.Open, FileAccess.Read), streamOptions), metadataOptions, utf8Decoder)
{
}
public PEFile(string fileName, Stream stream, PEStreamOptions streamOptions = PEStreamOptions.Default, MetadataReaderOptions metadataOptions = MetadataReaderOptions.Default, MetadataStringDecoder? utf8Decoder = null)
: this(fileName, new PEReader(stream, streamOptions), metadataOptions, utf8Decoder)
{
}
public PEFile(string fileName, PEReader reader, MetadataReaderOptions metadataOptions = MetadataReaderOptions.Default, MetadataStringDecoder? utf8Decoder = null)
: base(MetadataFileKind.PortableExecutable, fileName, reader, metadataOptions, utf8Decoder)
{
this.Reader = reader;
}
public override bool IsEmbedded => false;
public override int MetadataOffset => Reader.PEHeaders.MetadataStartOffset;
public override bool IsMetadataOnly => false;
protected override void Dispose(bool disposing)
{
if (disposing)
Reader.Dispose();
base.Dispose(disposing);
}
IModule TypeSystem.IModuleReference.Resolve(ITypeResolveContext context)
{
return new MetadataModule(context.Compilation, this, TypeSystemOptions.Default);
}
public override MethodBodyBlock GetMethodBody(int rva)
{
return Reader.GetMethodBody(rva);
}
public override SectionData GetSectionData(int rva)
{
return new SectionData(Reader.GetSectionData(rva));
}
public override int GetContainingSectionIndex(int rva)
{
return Reader.PEHeaders.GetContainingSectionIndex(rva);
}
ImmutableArray<SectionHeader> sectionHeaders;
public override ImmutableArray<SectionHeader> SectionHeaders {
get {
var value = sectionHeaders;
if (value.IsDefault)
{
value = Reader.PEHeaders.SectionHeaders
.Select(h => new SectionHeader {
Name = h.Name,
RawDataPtr = unchecked((uint)h.PointerToRawData),
RawDataSize = unchecked((uint)h.SizeOfRawData),
VirtualAddress = unchecked((uint)h.VirtualAddress),
VirtualSize = unchecked((uint)h.VirtualSize)
}).ToImmutableArray();
sectionHeaders = value;
}
return value;
}
}
public override CorHeader? CorHeader => Reader.PEHeaders.CorHeader;
}
}