mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
Merge pull request #334 from lmcintyre/unload
Unload plugins before unloading Dalamud to prevent crashes
This commit is contained in:
commit
8976bfec3f
4 changed files with 52 additions and 23 deletions
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -59,6 +59,11 @@ namespace Dalamud
|
|||
// Run session
|
||||
dalamud.Start();
|
||||
dalamud.WaitForUnload();
|
||||
if (dalamud.Framework.DispatchUpdateEvents)
|
||||
{
|
||||
dalamud.DisposePlugins();
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
|
||||
dalamud.Dispose();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -169,6 +169,7 @@ namespace Dalamud.Interface
|
|||
System.Threading.Thread.Sleep(500);
|
||||
|
||||
this.scene?.Dispose();
|
||||
this.setCursorHook.Dispose();
|
||||
this.presentHook.Dispose();
|
||||
this.resizeBuffersHook.Dispose();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue