Skip to content

Commit cf7b96f

Browse files
committed
okay, we gotta dispose stuff in this order
1 parent 2ab23d9 commit cf7b96f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/BizHawk.Client.Common/FileStreamWithTemp.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace BizHawk.Client.Common
77
{
88
internal class FileStreamWithTemp : IDisposable
99
{
10-
public readonly FileStream Stream;
10+
public FileStream Stream;
1111
public string FinalPath;
1212
public string TempPath;
1313

@@ -95,7 +95,9 @@ public FileWriteResult EndWrite()
9595

9696
public void Dispose()
9797
{
98-
Stream.Dispose();
98+
Stream?.Flush(flushToDisk: true);
99+
Stream?.Dispose();
100+
Stream = null;
99101

100102
// The caller should call EndWrite and handle potential failure.
101103
Debug.Assert(_finished, $"{nameof(FileWriteResult)} should not be disposed before calling {nameof(EndWrite)}");

src/BizHawk.Client.Common/FrameworkZipWriter.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public static FileWriteResult<FrameworkZipWriter> Create(string path, int compre
4949
public FileWriteResult EndWrite()
5050
{
5151
if (_fs == null) throw new ObjectDisposedException("Cannot use disposed ZipWriter.");
52+
53+
// We actually have to do this here since it has to be done before the file stream is closed.
54+
// (and it gets closed in EndWrite)
55+
_archive!.Dispose();
56+
_archive = null;
57+
5258
return _fs.EndWrite();
5359
}
5460

@@ -76,13 +82,13 @@ public void Dispose()
7682
if (_disposed) return;
7783
_disposed = true;
7884

79-
_archive!.Dispose();
85+
// _archive MUST be disposed before _fs. Or it throws.
86+
_archive?.Dispose();
8087
_archive = null;
8188
_zstd!.Dispose();
8289
_zstd = null;
8390

84-
_fs!.Stream.Flush(flushToDisk: true);
85-
_fs.Dispose();
91+
_fs!.Dispose();
8692
_fs = null;
8793
}
8894
}

0 commit comments

Comments
 (0)