Search Unity

Bug MissingReferenceException: ARAnchor is destroyed but still trying to access it.

Discussion in 'AR' started by Thimo_, Jan 1, 2021.

  1. Thimo_

    Thimo_

    Joined:
    Aug 26, 2019
    Posts:
    59
    Hi,

    In my code i delete the ARAnchor Gameobject at a certain time. When I do that I get the following error:
    "MissingReferenceException: The object of type 'ARAnchor' has been destroyed but you are still trying to access it."

    It happens on line 411 of the ARTrackablemanager class (trackable.transform.SetPositionAndRotation):

    Code (CSharp):
    1. void SetSessionRelativeData(TTrackable trackable, TSessionRelativeData data)
    2.         {
    3.             trackable.SetSessionRelativeData(data);
    4.             var worldSpacePose = sessionOrigin.trackablesParent.TransformPose(data.pose);
    5.             trackable.transform.SetPositionAndRotation(worldSpacePose.position, worldSpacePose.rotation);
    6.         }
    I believe after a bit of research its here and I think its happening in this method:

    Code (CSharp):
    1.  TTrackable CreateOrUpdateTrackable(TSessionRelativeData sessionRelativeData)
    2.         {
    3.             var trackableId = sessionRelativeData.trackableId;
    4.             if (m_Trackables.TryGetValue(trackableId, out var trackable))
    5.             {
    6.                 m_PendingAdds.Remove(trackableId);
    7.                 trackable.pending = false;
    8.                 SetSessionRelativeData(trackable, sessionRelativeData);
    9.             }
    10.             else
    11.             {
    12.                 trackable = CreateTrackable(sessionRelativeData);
    13.                 OnCreateTrackable(trackable);
    14.             }
    15.  
    16.             OnAfterSetSessionRelativeData(trackable, sessionRelativeData);
    17.             trackable.OnAfterSetSessionRelativeData();
    18.             return trackable;
    19.         }
    Is there something im doing wrong or is this a bug in AR Foundation that can be solved?

    Using AR Foundation 4.1.1 and calling the method "Object.Destroy(ARAnchor gameobject)"

    Delaying the destroy fixes the problem, but it feels like a hack
     
    Last edited: Jan 1, 2021
    KyryloKuzyk likes this.
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
  3. Thimo_

    Thimo_

    Joined:
    Aug 26, 2019
    Posts:
    59
    Last edited: Jan 17, 2021
  4. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
  5. Binary42

    Binary42

    Joined:
    Aug 15, 2013
    Posts:
    207
    Maybe related. When using Destroy(anchor.gameObject) as suggested, eventually, at some point the error below gets thrown once for every Anchor that has been destroyed until then.
    The old m_AnchorManager.RemoveAnchor(anchor) works with out issues.

    MissingReferenceException: The object of type 'ARAnchor' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].SetSessionRelativeData (TTrackable trackable, TSessionRelativeData data) (at Packages/com.unity.xr.arfoundation@4.2.2/Runtime/AR/ARTrackableManager.cs:394)
    UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].CreateOrUpdateTrackable (TSessionRelativeData sessionRelativeData) (at Packages/com.unity.xr.arfoundation@4.2.2/Runtime/AR/ARTrackableManager.cs:426)
    UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].Update () (at Packages/com.unity.xr.arfoundation@4.2.2/Runtime/AR/ARTrackableManager.cs:186)
     
    KyryloKuzyk likes this.
  6. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062
  7. Binary42

    Binary42

    Joined:
    Aug 15, 2013
    Posts:
    207
    Unfortunally i am not able to set up a minimum bare project with proper debugging.
    Here is how to recreate the error:

    Unity 2021.3.1f1
    with AR Core Template
    AR Foundation 4.2.2
    ARCore XR Plugin Version 4.2.2 (Installed 4.2.3)
    Platform to Android

    Open SampleScene
    Add a anchor prefab to AnchorCreator in the inspector
    To AnchorCreator.Awake() add
    InvokeRepeating("RemoveAllAnchors", 5, 5);
    BuildAndRun

    Tap to create anchors
    See them deleted every 5 seconds
    After a few rounds the deletion stops working

    Presumably because of the error i posted above, maybe someone can verify with debugger attached.

    MissingReferenceException: The object of type 'ARAnchor' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].SetSessionRelativeData (TTrackable trackable, TSessionRelativeData data) (at Packages/com.unity.xr.arfoundation@4.2.2/Runtime/AR/ARTrackableManager.cs:394)
    UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].CreateOrUpdateTrackable (TSessionRelativeData sessionRelativeData) (at Packages/com.unity.xr.arfoundation@4.2.2/Runtime/AR/ARTrackableManager.cs:426)
    UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].Update () (at Packages/com.unity.xr.arfoundation@4.2.2/Runtime/AR/ARTrackableManager.cs:186)