diff --git a/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs b/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs
index 6520ee4cf..2e5439ed0 100644
--- a/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs
+++ b/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs
@@ -94,6 +94,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
/// The listener to unregister.
internal void UnregisterListener(AddonLifecycleEventListener listener)
{
+ listener.IsRequestedToClear = true;
+
if (this.isInvokingListeners)
{
this.framework.RunOnTick(() => this.UnregisterListenerMethod(listener));
@@ -122,6 +124,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
{
foreach (var listener in globalListeners)
{
+ if (listener.IsRequestedToClear) continue;
+
try
{
listener.FunctionDelegate.Invoke(eventType, args);
@@ -138,6 +142,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
{
foreach (var listener in addonListener)
{
+ if (listener.IsRequestedToClear) continue;
+
try
{
listener.FunctionDelegate.Invoke(eventType, args);
diff --git a/Dalamud/Game/Addon/Lifecycle/AddonLifecycleEventListener.cs b/Dalamud/Game/Addon/Lifecycle/AddonLifecycleEventListener.cs
index fc82e0582..38c081e65 100644
--- a/Dalamud/Game/Addon/Lifecycle/AddonLifecycleEventListener.cs
+++ b/Dalamud/Game/Addon/Lifecycle/AddonLifecycleEventListener.cs
@@ -35,4 +35,9 @@ internal class AddonLifecycleEventListener
/// Gets the delegate this listener invokes.
///
public IAddonLifecycle.AddonEventDelegate FunctionDelegate { get; init; }
+
+ ///
+ /// Gets or sets if the listener is requested to be cleared.
+ ///
+ internal bool IsRequestedToClear { get; set; }
}
diff --git a/Dalamud/Game/Agent/AgentLifecycle.cs b/Dalamud/Game/Agent/AgentLifecycle.cs
index 45f0dec5c..1c895f9da 100644
--- a/Dalamud/Game/Agent/AgentLifecycle.cs
+++ b/Dalamud/Game/Agent/AgentLifecycle.cs
@@ -107,6 +107,8 @@ internal unsafe class AgentLifecycle : IInternalDisposableService
/// The listener to unregister.
internal void UnregisterListener(AgentLifecycleEventListener listener)
{
+ listener.IsRequestedToClear = true;
+
if (this.isInvokingListeners)
{
this.framework.RunOnTick(() => this.UnregisterListenerMethod(listener));
@@ -135,6 +137,8 @@ internal unsafe class AgentLifecycle : IInternalDisposableService
{
foreach (var listener in globalListeners)
{
+ if (listener.IsRequestedToClear) continue;
+
try
{
listener.FunctionDelegate.Invoke(eventType, args);
@@ -151,6 +155,8 @@ internal unsafe class AgentLifecycle : IInternalDisposableService
{
foreach (var listener in agentListener)
{
+ if (listener.IsRequestedToClear) continue;
+
try
{
listener.FunctionDelegate.Invoke(eventType, args);
diff --git a/Dalamud/Game/Agent/AgentLifecycleEventListener.cs b/Dalamud/Game/Agent/AgentLifecycleEventListener.cs
index 91f8aa3d3..592c126ba 100644
--- a/Dalamud/Game/Agent/AgentLifecycleEventListener.cs
+++ b/Dalamud/Game/Agent/AgentLifecycleEventListener.cs
@@ -35,4 +35,9 @@ public class AgentLifecycleEventListener
/// Gets the delegate this listener invokes.
///
public IAgentLifecycle.AgentEventDelegate FunctionDelegate { get; init; }
+
+ ///
+ /// Gets or sets if the listener is requested to be cleared.
+ ///
+ internal bool IsRequestedToClear { get; set; }
}