Skip to content

Commit

Permalink
Dispose AssemblyDefiniton objects when finished with them
Browse files Browse the repository at this point in the history
  • Loading branch information
bbepis committed Sep 18, 2020
1 parent 7c556e0 commit 8d4f63a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions AssemblyUnhollower/Contexts/RewriteGlobalContext.cs
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using AssemblyUnhollower.Extensions;
using Mono.Cecil;

namespace AssemblyUnhollower.Contexts
{
public class RewriteGlobalContext
public class RewriteGlobalContext : IDisposable
{
public UnhollowerOptions Options { get; }
private readonly Dictionary<string, AssemblyRewriteContext> myAssemblies = new Dictionary<string, AssemblyRewriteContext>();
Expand Down Expand Up @@ -33,7 +34,7 @@ public RewriteGlobalContext(UnhollowerOptions options, IEnumerable<string> sourc
var assemblyName = Path.GetFileNameWithoutExtension(sourceAssemblyPath);
if(assemblyName == "Il2CppDummyDll") continue;

var sourceAssembly = AssemblyDefinition.ReadAssembly(File.OpenRead(sourceAssemblyPath), new ReaderParameters(ReadingMode.Immediate) {MetadataResolver = metadataResolver});
var sourceAssembly = AssemblyDefinition.ReadAssembly(sourceAssemblyPath, new ReaderParameters(ReadingMode.Immediate) {MetadataResolver = metadataResolver});
myAssemblyResolver.Register(sourceAssembly);
var newAssembly = AssemblyDefinition.CreateAssembly(
new AssemblyNameDefinition(sourceAssembly.Name.Name.UnSystemify(), sourceAssembly.Name.Version),
Expand Down Expand Up @@ -80,5 +81,14 @@ public AssemblyRewriteContext GetAssemblyByName(string name)
{
return myAssemblies.TryGetValue(name, out var result) ? result : null;
}

public void Dispose()
{
foreach (var assembly in Assemblies)
{
assembly.NewAssembly.Dispose();
assembly.OriginalAssembly.Dispose();
}
}
}
}
2 changes: 2 additions & 0 deletions AssemblyUnhollower/Program.cs
Expand Up @@ -228,6 +228,8 @@ public static void Main(UnhollowerOptions options)
File.Copy(typeof(Decoder).Assembly.Location, Path.Combine(options.OutputDir, typeof(Decoder).Assembly.GetName().Name + ".dll"), true);

Console.WriteLine("Done!");

rewriteContext.Dispose();
}
}
}

0 comments on commit 8d4f63a

Please sign in to comment.