Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException caused by Invoke in onVesselChange #38

Open
BrettRyland opened this issue Feb 24, 2022 · 0 comments
Open

NullReferenceException caused by Invoke in onVesselChange #38

BrettRyland opened this issue Feb 24, 2022 · 0 comments

Comments

@BrettRyland
Copy link

Hi,
I'm seeing some exceptions caused by smartparts due to the Invoke call in onVesselChange on a part that I think is getting destroyed before the invoked function is finally called.

[ERR 11:06:30.073] Exception handling event onVesselChange in class Timer:System.NullReferenceException
  at (wrapper managed-to-native) UnityEngine.MonoBehaviour.InvokeDelayed(UnityEngine.MonoBehaviour,string,single,single)
  at UnityEngine.MonoBehaviour.Invoke (System.String methodName, System.Single time) [0x00001] in <2425394cf09642369e2109953e31f62b>:0 
  at Lib.Timer.onVesselChange (Vessel newVessel) [0x0001c] in <486db0c7375c47f7909494a00dbf2916>:0 
  at EventData`1[T].Fire (T data) [0x000b0] in <4deecb19beb547f19b1ff89b4c59bd84>:0 

[EXC 11:06:30.074] NullReferenceException
        UnityEngine.MonoBehaviour.Invoke (System.String methodName, System.Single time) (at <2425394cf09642369e2109953e31f62b>:0)
        Lib.Timer.onVesselChange (Vessel newVessel) (at <486db0c7375c47f7909494a00dbf2916>:0)
        EventData`1[T].Fire (T data) (at <4deecb19beb547f19b1ff89b4c59bd84>:0)
        UnityEngine.DebugLogHandler:LogException(Exception, Object)
        ModuleManager.UnityLogHandle.InterceptLogHandler:LogException(Exception, Object)
        UnityEngine.Debug:LogException(Exception)
        EventData`1:Fire(Vessel)
        FlightGlobals:setActiveVessel(Vessel, Boolean, Boolean)
        FlightGlobals:ForceSetActiveVessel(Vessel)
        BDArmory.UI.LoadedVesselSwitcher:ForceSwitchVessel(Vessel)
        BDArmory.UI.LoadedVesselSwitcher:UpdateCamera()
        BDArmory.UI.LoadedVesselSwitcher:Update()

Incidentally, the Unity documentation recommends using coroutines instead of invoke (https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html), which avoids these kinds of issues.
The pattern (in case you're unaware of it) is:

bool delayedFunctionRunning = false; // If required for exclusivity of running delayedFunction.
void onMyEvent()
{
    if (!delayedFunctionRunning)
    {
        StartCoroutine(delayedFunction(0.25f)); // Coroutines get stopped automatically if the instance is destroyed.
    }
}
IEnumerator delayedFunction(float delay)
{
    delayedFunctionRunning = true;
    yield return new WaitForSeconds(delay);
    if (part == null) yield break; // The part has been destroyed.
    // Do stuff.
    delayedFunctionRunning = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant