fix: Attempt to better handle hook disposal (#1803)

- Use a Weak Concurrent Collection to track scoped hooks
- Make `Hook`s remove themselves from the Tracked Hook list.
This commit is contained in:
KazWolfe 2025-03-13 14:16:28 -07:00 committed by GitHub
parent ee2c8dd9cc
commit 577977350f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 67 additions and 21 deletions

View file

@ -20,6 +20,8 @@ public sealed class AsmHook : IDisposable, IDalamudHook
private bool isEnabled = false;
private DynamicMethod statsMethod;
private Guid hookId = Guid.NewGuid();
/// <summary>
/// Initializes a new instance of the <see cref="AsmHook"/> class.
@ -44,7 +46,7 @@ public sealed class AsmHook : IDisposable, IDalamudHook
this.statsMethod.GetILGenerator().Emit(OpCodes.Ret);
var dele = this.statsMethod.CreateDelegate(typeof(Action));
HookManager.TrackedHooks.TryAdd(Guid.NewGuid(), new HookInfo(this, dele, Assembly.GetCallingAssembly()));
HookManager.TrackedHooks.TryAdd(this.hookId, new HookInfo(this, dele, Assembly.GetCallingAssembly()));
}
/// <summary>
@ -70,7 +72,7 @@ public sealed class AsmHook : IDisposable, IDalamudHook
this.statsMethod.GetILGenerator().Emit(OpCodes.Ret);
var dele = this.statsMethod.CreateDelegate(typeof(Action));
HookManager.TrackedHooks.TryAdd(Guid.NewGuid(), new HookInfo(this, dele, Assembly.GetCallingAssembly()));
HookManager.TrackedHooks.TryAdd(this.hookId, new HookInfo(this, dele, Assembly.GetCallingAssembly()));
}
/// <summary>
@ -116,6 +118,8 @@ public sealed class AsmHook : IDisposable, IDalamudHook
this.IsDisposed = true;
HookManager.TrackedHooks.TryRemove(this.hookId, out _);
if (this.isEnabled)
{
this.isEnabled = false;