fix hook leak
This commit is contained in:
@@ -33,8 +33,6 @@ namespace Pilz.Dalamud.Nameplates
|
|||||||
private Hook<AddonNamePlate_SetPlayerNameplateDetour>? hook_AddonNamePlate_SetPlayerNameplateDetour = null;
|
private Hook<AddonNamePlate_SetPlayerNameplateDetour>? hook_AddonNamePlate_SetPlayerNameplateDetour = null;
|
||||||
private unsafe delegate IntPtr AddonNamePlate_SetPlayerNameplateDetour(IntPtr playerNameplateObjectPtr, bool isTitleAboveName, bool isTitleVisible, IntPtr titlePtr, IntPtr namePtr, IntPtr freeCompanyPtr, IntPtr prefix, int iconId);
|
private unsafe delegate IntPtr AddonNamePlate_SetPlayerNameplateDetour(IntPtr playerNameplateObjectPtr, bool isTitleAboveName, bool isTitleVisible, IntPtr titlePtr, IntPtr namePtr, IntPtr freeCompanyPtr, IntPtr prefix, int iconId);
|
||||||
|
|
||||||
private bool allowHookHandling = false;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines if all hooks are enabled. If this is false, then there might be something wrong or the class already has been disposed.
|
/// Defines if all hooks are enabled. If this is false, then there might be something wrong or the class already has been disposed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -65,7 +63,7 @@ namespace Pilz.Dalamud.Nameplates
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Unhook();
|
Unhook(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +74,6 @@ namespace Pilz.Dalamud.Nameplates
|
|||||||
{
|
{
|
||||||
if (!IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
if (!IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
||||||
hook_AddonNamePlate_SetPlayerNameplateDetour?.Enable();
|
hook_AddonNamePlate_SetPlayerNameplateDetour?.Enable();
|
||||||
allowHookHandling = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -84,21 +81,38 @@ namespace Pilz.Dalamud.Nameplates
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal void Unhook()
|
internal void Unhook()
|
||||||
{
|
{
|
||||||
if (allowHookHandling && IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
Unhook(false);
|
||||||
hook_AddonNamePlate_SetPlayerNameplateDetour?.Disable();
|
}
|
||||||
allowHookHandling = false;
|
|
||||||
|
private void Unhook(bool isDisposing)
|
||||||
|
{
|
||||||
|
if (isDisposing)
|
||||||
|
{
|
||||||
|
if (!IsHookDisposed(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
||||||
|
hook_AddonNamePlate_SetPlayerNameplateDetour?.Disable();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
||||||
|
hook_AddonNamePlate_SetPlayerNameplateDetour?.Disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsHookDisposed<T>(Hook<T> hook) where T : Delegate
|
||||||
|
{
|
||||||
|
return hook == null || hook.IsDisposed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsHookEnabled<T>(Hook<T> hook) where T : Delegate
|
private static bool IsHookEnabled<T>(Hook<T> hook) where T : Delegate
|
||||||
{
|
{
|
||||||
return hook != null && hook.IsEnabled;
|
return !IsHookDisposed(hook) && hook.IsEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntPtr SetPlayerNameplateDetour(IntPtr playerNameplateObjectPtr, bool isTitleAboveName, bool isTitleVisible, IntPtr titlePtr, IntPtr namePtr, IntPtr freeCompanyPtr, IntPtr prefix, int iconId)
|
private IntPtr SetPlayerNameplateDetour(IntPtr playerNameplateObjectPtr, bool isTitleAboveName, bool isTitleVisible, IntPtr titlePtr, IntPtr namePtr, IntPtr freeCompanyPtr, IntPtr prefix, int iconId)
|
||||||
{
|
{
|
||||||
var result = IntPtr.Zero;
|
var result = IntPtr.Zero;
|
||||||
|
|
||||||
if (allowHookHandling && IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
if (IsHookEnabled(hook_AddonNamePlate_SetPlayerNameplateDetour))
|
||||||
{
|
{
|
||||||
var eventArgs = new AddonNamePlate_SetPlayerNameEventArgs
|
var eventArgs = new AddonNamePlate_SetPlayerNameEventArgs
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user