Skip to content

Commit 1802e6f

Browse files
committed
v1.9.9: Fix FileStream_NET finalizer shenans
1 parent f083554 commit 1802e6f

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

AL_Common/IO/FileStream_NET.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public sealed class FileStream_NET : Stream
2323
private int _readPos;
2424
private int _readLen;
2525

26+
#if false
2627
public FileStream_NET(AL_SafeFileHandle handle, FileAccess access, byte[] buffer, int bufferSize)
2728
{
2829
ValidateHandle(handle, access);
@@ -45,6 +46,7 @@ public FileStream_NET(AL_SafeFileHandle handle, FileAccess access, byte[] buffer
4546

4647
_fileHandle = handle;
4748
}
49+
#endif
4850

4951
public FileStream_NET(string path, FileMode mode, FileAccess access, FileShare share, byte[] buffer, int bufferSize)
5052
: this(path, mode, access, share, buffer, bufferSize, FileOptions.None)
@@ -56,13 +58,17 @@ public FileStream_NET(string path, FileMode mode, FileAccess access, FileShare s
5658
{
5759
}
5860

61+
// Disable because we don't need it and we don't want it running when we're in a weird state and _fileHandle
62+
// could be null etc.
63+
#if false
5964
~FileStream_NET()
6065
{
6166
// Preserved for compatibility since FileStream has defined a
6267
// finalizer in past releases and derived classes may depend
6368
// on Dispose(false) call.
6469
Dispose(false);
6570
}
71+
#endif
6672

6773
private FileStream_NET(string path, FileMode mode, FileAccess access, FileShare share, byte[] buffer, int bufferSize, FileOptions options, long preallocationSize)
6874
{
@@ -75,10 +81,14 @@ private FileStream_NET(string path, FileMode mode, FileAccess access, FileShare
7581

7682
_access = access;
7783

78-
_fileHandle = AL_SafeFileHandle.Open(fullPath, mode, access, share, options, preallocationSize);
79-
8084
try
8185
{
86+
/*
87+
IMPORTANT: This MUST go inside the try block as it can throw!
88+
The modern .NET version, through a complicated chain of logic, avoids this problem, but we don't.
89+
The finalizer will run and _fileHandle will be null and then we crash the whole app.
90+
*/
91+
_fileHandle = AL_SafeFileHandle.Open(fullPath, mode, access, share, options, preallocationSize);
8292
if (mode == FileMode.Append && CanSeek)
8393
{
8494
_appendStart = _filePosition = GetLengthCore;
@@ -92,8 +102,12 @@ private FileStream_NET(string path, FileMode mode, FileAccess access, FileShare
92102
{
93103
// If anything goes wrong while setting up the stream, make sure we deterministically dispose
94104
// of the opened handle.
95-
_fileHandle.Dispose();
96-
_fileHandle = null!;
105+
_fileHandle?.Dispose();
106+
// If we want to enable the ctor that takes a file handle, we need to re-enable this and then re-do
107+
// the logic to expect that this could null in the finalizer if we enable that too.
108+
#if false
109+
_fileHandle = null;
110+
#endif
97111
throw;
98112
}
99113
}

AngelLoader/AngelLoader.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
<NeutralLanguage>en-US</NeutralLanguage>
2424
<Description>A standalone fan mission loader and manager for Thief Gold, Thief II, Thief: Deadly Shadows, System Shock 2, and The Dark Mod.</Description>
2525
<Copyright>Copyright © 2018 - 2024</Copyright>
26-
<Version>1.9.8</Version>
27-
<AssemblyVersion>1.9.8</AssemblyVersion>
28-
<FileVersion>1.9.8</FileVersion>
26+
<Version>1.9.9</Version>
27+
<AssemblyVersion>1.9.9</AssemblyVersion>
28+
<FileVersion>1.9.9</FileVersion>
2929
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
3030
</PropertyGroup>
3131

Localizable_Strings_Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ If you want to update a translation from several versions back and don't want to
66

77
## List of localization-related changes by version
88

9+
### v1.9.9:
10+
11+
No localizable text changes.
12+
913
### v1.9.8:
1014

1115
No localizable text changes.

0 commit comments

Comments
 (0)