Skip to content

Commit

Permalink
fix leaked handle with registering bmd
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanstuck committed Mar 26, 2024
2 parents a412902 + 20f55a1 commit b21825b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Emulator/AWB.Stream.Emulator/ModConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ModR2RManagedDll64": "x64/AWB.Stream.Emulator.dll",
"ModNativeDll32": "",
"ModNativeDll64": "",
"IsLibrary": false,
"IsLibrary": true,
"ReleaseMetadataFileName": "AWB.Stream.Emulator.ReleaseMetadata.json",
"PluginData": {
"GitHubDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion Emulator/BMD.File.Emulator/BmdEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ public void OnModLoading(string modFolder)
/// Invalidates a BMD file with a specified name.
/// </summary>
/// <param name="bmdPath">Full path to the file.</param>
public void UnregisterFile(string bmdPath) => _pathToStream!.Remove(bmdPath, out _);
public void UnregisterFile(string bmdPath)
{
_pathToStream!.Remove(bmdPath, out var stream);
stream?.Dispose();
}

public void RegisterFile(string destinationPath, Stream stream)
{
Expand Down
9 changes: 3 additions & 6 deletions Emulator/BMD.File.Emulator/BmdEmulatorApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ public void RegisterBmd(string sourcePath, string destinationPath)

Native.SetFilePointerEx(handle, 0, IntPtr.Zero, 0);

var fileStream = new FileStream(new SafeFileHandle(handle, false), FileAccess.Read);
var stream = StreamUtils.CreateMemoryStream(fileStream.Length);
fileStream.CopyTo(stream);

var emulated = new EmulatedFile<Stream>(stream);
_bmdEmulator.RegisterFile(destinationPath, stream);
var fileStream = new FileStream(new SafeFileHandle(handle, true), FileAccess.Read);
var emulated = new EmulatedFile<FileStream>(fileStream);
_bmdEmulator.RegisterFile(destinationPath, fileStream);
_framework.RegisterVirtualFile(destinationPath, emulated, false);

_logger.Info("[BmdEmulatorApi] Registered bmd {0} at {1}", sourcePath, destinationPath);
Expand Down

0 comments on commit b21825b

Please sign in to comment.