Skip to content

Commit

Permalink
Merge pull request #257 from timcassell/fix-nullref-onapplicationquit
Browse files Browse the repository at this point in the history
Reset static state from SubsystemRegistration
  • Loading branch information
timcassell authored Sep 5, 2023
2 parents 78eef35 + 09084c3 commit 6adf519
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Package/UnityHelpers/Internal/PromiseBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ internal static void InitializePromiseConfig()
PromiseBehaviour.Initialize();
}

// AppDomain reload could be disabled in editor, so we need to explicitly reset static fields.
// See https://github.com/timcassell/ProtoPromise/issues/204
// https://docs.unity3d.com/Manual/DomainReloading.html
[RuntimeInitializeOnLoadMethod((RuntimeInitializeLoadType) 4)] // SubsystemRegistration
internal static void ResetStaticState()
{
PromiseBehaviour.ResetStaticState();
}

#if !PROTO_PROMISE_DEVELOPER_MODE
[DebuggerNonUserCode, StackTraceHidden]
#endif
Expand Down Expand Up @@ -108,6 +117,7 @@ private void OnDestroy()
{
UnityEngine.Debug.LogError("PromiseBehaviour destroyed! Removing PromiseSynchronizationContext from Promise.Config.ForegroundContext. PromiseYielder functions will stop working.");
ResetConfig();
ResetStaticState();
}
}

Expand Down Expand Up @@ -155,8 +165,6 @@ private void OnApplicationQuit()
_isApplicationQuitting = true;
if (Application.isEditor & s_instance == this)
{
// AppDomain reload could be disabled in editor, so we need to explicitly reset static fields. See https://github.com/timcassell/ProtoPromise/issues/204
ResetProcessors();
ResetConfig();
// Destroy this to prevent a memory leak.
Destroy(this);
Expand All @@ -167,7 +175,6 @@ private void OnApplicationQuit()

private void ResetConfig()
{
s_instance = null;
if (Promise.Config.ForegroundContext == _syncContext)
{
Promise.Config.ForegroundContext = null;
Expand All @@ -186,6 +193,15 @@ private void ResetConfig()
}
_syncContext.Execute(); // Clear out any pending callbacks.
}

internal static void ResetStaticState()
{
if (!ReferenceEquals(s_instance, null))
{
s_instance.ResetProcessors();
s_instance = null;
}
}
}
}
}

0 comments on commit 6adf519

Please sign in to comment.