Closed tomkrikorian closed 2 years ago
Hi @tomkrikorian, thank you for sharing with us the issue and your suggestion. Could you please help us to understand how you reset the ARSession when getting the focus of the app back? Are you able to reproduce the issue with the sample code (plus some modification)? Maybe some code snippet and highlighting which operations before and after the app's pause/resume states can help us to reproduce.
Hi @phamqduc !
So for the full context: Not sure it's relevant but we are using the Unity as a Library feature. So unlike a typical unity application, we don't use the OnApplicationFocus method of a MonoBehavior to know if we got the focus of the app back but we instead call our method to reset our ARSession when we receive the applicationWillEnterForeground event from the native side.
This method basically do this:
_spatialAnchorManager.DestroySession();
arSession.Reset();
await UniTask.WaitUntil(() => ARSession.state == ARSessionState.SessionTracking);
await StartSession();
I'll try to reproduce using your samples, I cannot reproduce on my side sadly but we received crash reports on TestFlight about this a few times from some of our beta testers.
My current guess is that in some cases SpatialAnchorManager does not properly handle when an ARSession is destroyed and created again. One example is that the ARSessionInitialized
member is never set to false.
So if the ARSession is not yet ready at that moment in CreateSessionAsync
:
// Wait for a valid AR Session in case we are starting before AR Foundation is ready.
if (ARSession.state == ARSessionState.SessionTracking)
{
CompleteARFoundationInitialization();
}
We will never set the session properly again here:
private void ARSession_stateChanged(ARSessionStateChangedEventArgs obj)
{
Debug.Log($"ARSession state changed to {obj.state}");
if (obj.state == ARSessionState.SessionTracking && !ARSessionInitialized)
{
CompleteARFoundationInitialization();
}
}
It does not happen in our case because we properly await the ARSession in our code since Day 1 here:
await UniTask.WaitUntil(() => ARSession.state == ARSessionState.SessionTracking);
But i'm sure if we comment that code, we will get issues.
Hi @tomkrikorian, thanks for sharing your insights into how resetting ARSession
may have impact on SpatialAnchorManager
.
ARSessionInitialized
poses one issue. I created an internal thread (#37551878) to track it.
I tried out replacing CloudManager.ResetSessionAsync
in step (case AppState.DemoStepStopSession
) with
CloudManager.DestroySession();
#if UNITY_ANDROID || UNITY_IOS
ARSession arSession = FindObjectOfType<ARSession>();
arSession.Reset();
#endif
of the Basic Demo and then re-creating/configuring session as usual. So far, I have not been able to produce a crash yet (unfortunately, testing is only on Unity Android sample - if you suspect different behavior on iOS, please let us know too).
Please kindly update us on how you can re-produce the crash on our sample code. Thanks.
Hi @phamqduc, The issue happens when you lose focus of the app, i'm not sure resetting the ARSession during this step can really reproduce our issue. I'll give you a repro project as soon as i have the time to work on this issue again. We don't have an Android version of our app, i can only confirm that it does happen on iOS so i would suggest to try to reproduce the issue on iOS.
Hi @tomkrikorian - apologies for the delay - tracking this under work item 37551878
Hi All, I'm facing a similar crash when resetting the AR Session before CreateAnchor
ASA version 2.11.0 Unity - 2020.3.25f1 iOS 15.2, iPhone 13 Pro
The crash happens randomly when creating an Anchor, here are logs
Crashed: UnityGfxDeviceWorker
0 libobjc.A.dylib 0x33f0 objc_retain + 16
1 UnityFramework 0x9dc884 TexturesMetal::AddCreatedTexture(TextureID, id<MTLTexture>, bool) + 32
2 UnityFramework 0x94934c GfxDeviceWorker::RunCommand(ThreadedStreamBuffer&) + 13164
3 UnityFramework 0x9b2974 GfxDeviceWorkerAutoreleasePoolProxy + 68
4 UnityFramework 0x94ddcc GfxDeviceWorker::RunExt(ThreadedStreamBuffer&) + 92
5 UnityFramework 0x945ce0 GfxDeviceWorker::RunGfxDeviceWorker(void*) + 12
6 UnityFramework 0x69ac74 Thread::RunThreadWrapper(void*) + 76
7 libsystem_pthread.dylib 0x19a4 _pthread_start + 148
8 libsystem_pthread.dylib 0xea0 thread_start + 8
Hello - could you please try the resolution provided by @msftradford here: https://github.com/Azure/azure-spatial-anchors-samples/issues/284 Please let us know if that resolves your issues, thank you!
Hi @Varnauld I fixed the crash by disabling AR Foundation occlusion in Unity for iOS, it seems running AR Foundation occlusion and Azure session at the same time was causing the crash
Thank you for the update @nikhilsawlani ! @tomkrikorian are you still having issues?
Hello, As we haven't heard back, we will close this issue, but please do reach out if you have any follow up questions or further issues, thank you!
Description
Unity crashes when resetting the ARSession (with ARSession.Reset method) when getting the focus of the app back (example : Locking your phone & Unlocking it then resuming the app). The crash happens in the
ProcessLatestFrame
method ofSpatialAnchorManager
and when trying to Process the frame.Looks like there should be a check to see if the nativePtr is good ?
Development information (please complete the following information)
AR Device information (please complete the following information):
Additional context
Here is the backtrace :