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,6 +351,26 @@ namespace Dalamud
this.finishUnloadSignal?.WaitOne();
}
public void DisposePlugins()
{
// 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
// a render call after it has been disposed, which can crash if it attempts to
// use any resources that it freed in its own Dispose method
this.InterfaceManager?.Dispose();
try
{
this.PluginManager.UnloadPlugins();
}
catch (Exception ex)
{
Log.Error(ex, "Plugin unload failed.");
}
this.DalamudUi?.Dispose();
}
/// <summary>
/// Dispose Dalamud subsystems.
/// </summary>
@ -358,27 +378,6 @@ namespace Dalamud
{
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
// 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
// use any resources that it freed in its own Dispose method
this.InterfaceManager?.Dispose();
try
{
this.PluginManager.UnloadPlugins();
}
catch (Exception ex)
{
Log.Error(ex, "Plugin unload failed.");
}
this.DalamudUi?.Dispose();
this.Framework?.Dispose();
this.ClientState?.Dispose();

View file

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

View file

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

View file

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