Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug ARTrackableManager spamming NullReferenceException

Discussion in 'AR' started by Pitou22, Nov 10, 2023.

  1. Pitou22

    Pitou22

    Joined:
    Sep 23, 2015
    Posts:
    48
    Hello,

    I'm developping an application for Hololens 2, with Unity 2023.1.16f1, AR Foundation 5.1.0, OpenXR Plugin 1.8.2 and Mixed Reality OpenXR Plugin 1.9.0.

    I use multiples ARAnchor to anchor a 3D model in real environment. I also use an XRAnchorStore and an XRAnchorTransferBatch to persist and share anchors across multiple Hololens.

    For some reason I ignore, sometimes the XROrigin starts to spam NullReferenceException (1 at each frame, the Hololens starts to lag):

    It comes from this in
    XROrigin.OnBeforeRender
    :

    Code (CSharp):
    1. if (TrackablesParent.hasChanged)
    2. {
    3.     TrackablesParentTransformChanged?.Invoke(new ARTrackablesParentTransformChangedEventArgs(this, TrackablesParent));
    4.     TrackablesParent.hasChanged = false;
    5. }
    Because there is an excption in the event triggered, the
    TrackablesParent.hasChanged
    flag is never set to false...

    I think one of my anchor has been destroyed but not removed from the trackables list in the
    ARTrackableManager.OnTrackablesParentTransformChanged
    function (the
    ARTrackableManager
    is the base class of the
    ARAnchorManager
    I use).

    Code (CSharp):
    1. void OnTrackablesParentTransformChanged(ARTrackablesParentTransformChangedEventArgs eventArgs)
    2. {
    3.      foreach (var trackable in trackables)
    4.      {
    5.          var transform = trackable.transform; // I think here trackable is null
    6.          if (transform.parent != eventArgs.TrackablesParent)
    7.          {
    8.              var desiredPose = eventArgs.TrackablesParent.TransformPose(trackable.sessionRelativePose);
    9.              transform.SetPositionAndRotation(desiredPose.position, desiredPose.rotation);
    10.          }
    11.      }
    12. }
    Does anyone know this issue or from where it comes?
    We don't have access to the protected
    m_Trackables
    dictionnary, destroyed anchors should have been removed automatically (most of the time, it works). I'm still investigating, but I don't have a constant bug reproduction.
     
  2. Pitou22

    Pitou22

    Joined:
    Sep 23, 2015
    Posts:
    48
    It seems the issue comes from World Locking Tools (https://learn.microsoft.com/en-us/mixed-reality/world-locking-tools/)

    The spaming starts when World Locking Tools moves the XROrigin to compensate for Hololens drift: the trackables parent transform has changed and the exception loop starts.

    When I disable World Locking Tools, I don't have anymore null trackables in the ARAnchorManager trackables list.

    Side note: I have another issue with WLT. When active, the
    ARAnchorManager
    event
    OnAnchorsChanged(ARAnchorsChangedEventArgs eventArgs)
    doesn't contains the added anchors...
     
  3. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    989
    FYI This looks like a question for Microsoft at first glance. Note our FAQ:

     
    Pitou22 likes this.
  4. kankane

    kankane

    Joined:
    Feb 22, 2013
    Posts:
    23
    Have you managed to sort out this issue? I have the same error popping up, but I am not using Hololens. I did however insert another camera offset in between XROrigin and Camera Offset for the purposes of tweaking the camera offset. I am assuming WLT does the same. Inserting another offset is described here so I am assuming it should work:
    https://docs.unity3d.com/Packages/c...res/device-tracking.html#gameobject-hierarchy
     
  5. Pitou22

    Pitou22

    Joined:
    Sep 23, 2015
    Posts:
    48
    Yes, I fixed it: I removed WLT from the project
    It wasn't so usefull, and I got the feeling you shouldn't place your own anchors when WLT is active.