Merge pull request #334 from lmcintyre/unload

Unload plugins before unloading Dalamud to prevent crashes
This commit is contained in:
goaaats 2021-04-26 20:02:46 +02:00 committed by GitHub
commit 8976bfec3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 23 deletions

View file

@ -351,17 +351,8 @@ namespace Dalamud
this.finishUnloadSignal?.WaitOne(); this.finishUnloadSignal?.WaitOne();
} }
/// <summary> public void DisposePlugins()
/// Dispose Dalamud subsystems.
/// </summary>
public void Dispose()
{ {
try
{
// this must be done before unloading plugins, to prevent crashes due to errors
// in plugin cleanup
this.Framework.DispatchUpdateEvents = false;
// this must be done before unloading plugins, or it can cause a race condition // this must be done before unloading plugins, or it can cause a race condition
// due to rendering happening on another thread, where a plugin might receive // due to rendering happening on another thread, where a plugin might receive
// a render call after it has been disposed, which can crash if it attempts to // a render call after it has been disposed, which can crash if it attempts to
@ -378,7 +369,15 @@ namespace Dalamud
} }
this.DalamudUi?.Dispose(); this.DalamudUi?.Dispose();
}
/// <summary>
/// Dispose Dalamud subsystems.
/// </summary>
public void Dispose()
{
try
{
this.Framework?.Dispose(); this.Framework?.Dispose();
this.ClientState?.Dispose(); this.ClientState?.Dispose();

View file

@ -59,6 +59,11 @@ namespace Dalamud
// Run session // Run session
dalamud.Start(); dalamud.Start();
dalamud.WaitForUnload(); dalamud.WaitForUnload();
if (dalamud.Framework.DispatchUpdateEvents)
{
dalamud.DisposePlugins();
Thread.Sleep(100);
}
dalamud.Dispose(); dalamud.Dispose();
} }

View file

@ -28,6 +28,8 @@ namespace Dalamud.Game.Internal {
public delegate IntPtr OnDestroyDelegate(); public delegate IntPtr OnDestroyDelegate();
public delegate bool OnRealDestroyDelegate(IntPtr framework);
/// <summary> /// <summary>
/// Event that gets fired every time the game framework updates. /// Event that gets fired every time the game framework updates.
/// </summary> /// </summary>
@ -37,6 +39,8 @@ namespace Dalamud.Game.Internal {
private Hook<OnDestroyDetour> destroyHook; private Hook<OnDestroyDetour> destroyHook;
private Hook<OnRealDestroyDelegate> realDestroyHook;
/// <summary> /// <summary>
/// A raw pointer to the instance of Client::Framework /// A raw pointer to the instance of Client::Framework
/// </summary> /// </summary>
@ -101,6 +105,10 @@ namespace Dalamud.Game.Internal {
var pDestroy = Marshal.ReadIntPtr(vtable, IntPtr.Size * 3); var pDestroy = Marshal.ReadIntPtr(vtable, IntPtr.Size * 3);
this.destroyHook = this.destroyHook =
new Hook<OnDestroyDetour>(pDestroy, new OnDestroyDelegate(HandleFrameworkDestroy), this); new Hook<OnDestroyDetour>(pDestroy, new OnDestroyDelegate(HandleFrameworkDestroy), this);
var pRealDestroy = Marshal.ReadIntPtr(vtable, IntPtr.Size * 2);
this.realDestroyHook =
new Hook<OnRealDestroyDelegate>(pRealDestroy, new OnRealDestroyDelegate(HandleRealDestroy), this);
} }
public void Enable() { public void Enable() {
@ -109,6 +117,7 @@ namespace Dalamud.Game.Internal {
this.updateHook.Enable(); this.updateHook.Enable();
this.destroyHook.Enable(); this.destroyHook.Enable();
this.realDestroyHook.Enable();
} }
public void Dispose() { public void Dispose() {
@ -117,6 +126,7 @@ namespace Dalamud.Game.Internal {
this.updateHook.Dispose(); this.updateHook.Dispose();
this.destroyHook.Dispose(); this.destroyHook.Dispose();
this.realDestroyHook.Dispose();
} }
private bool HandleFrameworkUpdate(IntPtr framework) { private bool HandleFrameworkUpdate(IntPtr framework) {
@ -168,11 +178,23 @@ namespace Dalamud.Game.Internal {
return this.updateHook.Original(framework); return this.updateHook.Original(framework);
} }
private IntPtr HandleFrameworkDestroy() { private bool HandleRealDestroy(IntPtr framework)
Log.Information("Framework::OnDestroy!"); {
if (this.DispatchUpdateEvents)
{
Log.Information("Framework::Destroy!");
this.dalamud.DisposePlugins();
Log.Information("Framework::Destroy OK!");
}
this.DispatchUpdateEvents = false; this.DispatchUpdateEvents = false;
return this.realDestroyHook.Original(framework);
}
private IntPtr HandleFrameworkDestroy() {
Log.Information("Framework::Free!");
// Store the pointer to the original trampoline location // Store the pointer to the original trampoline location
var originalPtr = Marshal.GetFunctionPointerForDelegate(this.destroyHook.Original); var originalPtr = Marshal.GetFunctionPointerForDelegate(this.destroyHook.Original);
@ -180,6 +202,8 @@ namespace Dalamud.Game.Internal {
this.dalamud.WaitForUnloadFinish(); this.dalamud.WaitForUnloadFinish();
Log.Information("Framework::Free OK!");
// Return the original trampoline location to cleanly exit // Return the original trampoline location to cleanly exit
return originalPtr; return originalPtr;
} }

View file

@ -169,6 +169,7 @@ namespace Dalamud.Interface
System.Threading.Thread.Sleep(500); System.Threading.Thread.Sleep(500);
this.scene?.Dispose(); this.scene?.Dispose();
this.setCursorHook.Dispose();
this.presentHook.Dispose(); this.presentHook.Dispose();
this.resizeBuffersHook.Dispose(); this.resizeBuffersHook.Dispose();
} }