Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

App crashes after ARSession.Reset()

Discussion in 'AR' started by ziemlich3D, Oct 18, 2018.

  1. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Hello everyone,

    I'm using markerless Augmented Reality in my current project. The tracking and coding works very well for both plattformes iOS & Android, thanks to ARFoundation. But I encountered a weird problem, when resetting the ARSession: If I call Reset() at the ARSessionScript for the first time it mostly works, but the second time I invoke it the app instantly crashes on both plattformes iOS & Android. I think it should be very easy by just calling Reset() and that's it? Or did I miss something else to take care of?
    Here is the log (with anonymized product-title) from the Android-device:

    10-18 10:40:08.697: I/native(8478): session_c_api.cc:539 Deleted ArSession.
    10-18 10:40:08.699: E/CRASH(8478): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0000000c
    10-18 10:40:08.699: E/CRASH(8478): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    10-18 10:40:08.699: E/CRASH(8478): Build fingerprint: 'samsung/heroltexx/herolte:8.0.0/R16NW/G930FXXS3ERHD:user/release-keys'
    10-18 10:40:08.699: E/CRASH(8478): Revision: '8'
    10-18 10:40:08.699: E/CRASH(8478): pid: 8478, tid: 8505, name: UnityMain >>> com.customer.product <<<
    10-18 10:40:08.699: E/CRASH(8478): r0 c0b13fc0 r1 0513ac47 r2 e88aa380 r3 c6983388
    10-18 10:40:08.699: E/CRASH(8478): r4 c0b13fc0 r5 00000004 r6 00000000 r7 ba5d5918
    10-18 10:40:08.700: E/CRASH(8478): r8 bc262ed8 r9 c6e7ede4 sl c6e7ecf8 fp c6e7e580
    10-18 10:40:08.700: E/CRASH(8478): ip bc66ddd4 sp c6e7e548 lr bc5d470c pc bc5e1204 cpsr e9670c90
    10-18 10:40:08.700: E/CRASH(8478): backtrace:
    10-18 10:40:09.309: E/CRASH(8478): #00 pc 00015204 /data/app/com.customer.product-1BIzshyOpS3m_WTggBh9KQ==/lib/arm/libARExtensions.so (CameraImageApi::ImageManager::~ImageManager()+20)
    10-18 10:40:09.309: E/CRASH(8478): #01 pc 00008708 /data/app/com.customer.product-1BIzshyOpS3m_WTggBh9KQ==/lib/arm/libARExtensions.so (CameraImageApi::Destroy()+20)
    10-18 10:40:09.309: E/CRASH(8478): #02 pc 00406658 /data/app/com.customer.product-1BIzshyOpS3m_WTggBh9KQ==/lib/arm/libunity.so
    10-18 10:40:09.309: E/CRASH(8478): #03 pc 0041b36c /data/app/com.customer.product-1BIzshyOpS3m_WTggBh9KQ==/lib/arm/libunity.so
    10-18 10:40:09.309: E/CRASH(8478): #04 pc 00007f3c ( (wrapper managed-to-native) UnityEngine.Experimental.SubsystemManager:DestroyInstance_Internal (intptr) + 0x3c (0xd58e7f00 0xd58e7f74) [0xe9126ee0 - Unity Root Domain]+32572)
    10-18 10:40:09.309: E/CRASH(8478): #05 il ........ at (wrapper managed-to-native) UnityEngine.Experimental.SubsystemManager.DestroyInstance_Internal (intptr) <0xffffffff>
    10-18 10:40:09.309: E/CRASH(8478): #06 il 00000030 at UnityEngine.Experimental.Subsystem.Destroy () <0x00030>
    10-18 10:40:09.309: E/CRASH(8478): #07 il 00000027 at UnityEngine.XR.ARFoundation.ARSubsystemManager.DestroySubsystem (UnityEngine.Experimental.Subsystem) <0x00027>
    10-18 10:40:09.309: E/CRASH(8478): #08 il 00000043 at UnityEngine.XR.ARFoundation.ARSubsystemManager.DestroySubsystems () <0x00043>
    10-18 10:40:09.309: E/CRASH(8478): #09 il 0000002f at UnityEngine.XR.ARFoundation.ARSession.Reset () <0x0002f>

    The whole log can be found in the file attached to this post.

    I'm using the following packages:
    • AR Foundation 1.0.0-preview.19
    • ARCore XR Plugin 1.0.0-preview.20
    • ARKit XR Plugin 1.0.0-preview.16
    • Unity 2018.2.12f1 with Pro-Subscription
    Hardware I'm using for testing:
    • Samsung Galaxy S7
    • iPad 9,7'' 2017

    Could anybody give me a hint or a solution on how I could eliminate this problem? I'm running out of ideas how to solve this.
    Thank you :)
     

    Attached Files:

    dyuldashev likes this.
  2. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    74
    Wow, was just going to ask the same question a few hours ago. I am in a similar situation. I have a menu scene, and three AR scenes(AR Foundation). The first time I run one of the scenes, it's all good. I go back to the main menu. I pick another scene, and now, the tracking is not so stable. Worst of all, it crashes when I try to go back to the main menu. In my opinion, this has to do with with the Reset() function that you're referring to since it might be called when switching between scenes.

    In my opinion, here's what's happening. When it tries to reset, it has to unload some AR assets. It's unable to do so. Why I think that way is simple: for the first time, it all works great. The second time, on the other hand, we start to have some issues. It just means that there is something going wrong right when we reset the AR functionality....
     
  3. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    You are right! I was so focused on the crash in the AR-scene that I didn't check further: I'm also having a menu-scene in my setup, where the user can switch to the AR-scene and back to the menu-scene. The switch from AR- to menu-scene can be done once and the second time the app also crashes instantly, right as your app does.
    That's what I found: If you switch scenes then this will be called in ARSession: First OnDisable() and after that OnDestroy()

    void OnDisable()
    {
    ARSubsystemManager.StopSubsystems();
    }

    void OnDestroy()
    {
    ARSubsystemManager.DestroySubsystems();
    }


    That's the first half of the code, which is called in Reset()

    public void Reset()
    {
    if (ARSubsystemManager.systemState < ARSystemState.Ready)
    return;

    ARSubsystemManager.StopSubsystems();
    ARSubsystemManager.DestroySubsystems();
    ...
    }


    Looking at the code above makes me think that your theory is absolutely plausible with locating the problems at cleaning up some internal assets before restarting the tracking or switching the scene.
     
  4. viktor_chernikov

    viktor_chernikov

    Joined:
    Oct 8, 2018
    Posts:
    10
    I have the very same crash, except in my case there's no scene switching. I have one scene with two modes: basic 3D and AR. When I try entering AR for the second time the session object is reused and reset is called. Before that I simply recreated ARSession every time and it worked without a hitch. Ever since I moved away from this flow I have this crash.
     
  5. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    74
    Also, here's a visual order in which the crash happens on iOS:
    UnloadGameSceneError.png
    Here, you can see that the crash is happening because of AR Foundation, and I am not sure whether it exist on Android phones or not. It starts when you try to unload the scene, and I guess it's trying to delete the AR Camera Provider objects, which was already destroyed per this crash log. Maybe, it allocates this object just once. But yeah, it's a bug, that's for sure. If you find a solution, please, let us know. In the meantime, @jimmya @tdmowrer, would you please take a look at this issue.
     
  6. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    I have also two modes (3D and AR) in the affected scene. If I switch between the modes, I just completely deactivate the GameObjects, which are related to AR or 3D. This deactivation doesn't make much trouble, though, as I can switch between modes very often without a crash (refering to the documentation the AR-session gets paused by deactivating the GameObject and is resumed after activation). Sometimes the AR-mode is then a little bit laggy, but that occures only very rarely. Maybe you can do it also that way.

    To get help from Unity-devs I just created a little example-project based on the ARFoundation-samples and submitted a bug-report.

    The behaviour of the sample project is exact the same, as in dyuldashev's project, your project and my bigger project: Reset() twice -> crash; switch AR->other scene twice -> crash.
    Hopefully they will investigate the bug-report soon and find the cause for this :)
     
    Last edited: Oct 23, 2018
  7. viktor_chernikov

    viktor_chernikov

    Joined:
    Oct 8, 2018
    Posts:
    10
    It looks like a different issue. There's no Reset call in the call stack, and the one with Reset ends up with ImageManager destructor. Have no idea what's causing it in your case

    Eventually I ended up not calling reset at all. I'm not sure, but I think in my case it is safe to do as I create the whole AR-related object hierarchy from the code. The crash went away and switching speed between the modes shot up.
     
  8. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    But don't you agree, to what I wrote in my second post in this thread the way some functions are called is very similar regarding Reset() and cleanup before scene-change.
    I'm talking about

    ARSubsystemManager.StopSubsystems();
    ARSubsystemManager.DestroySubsystems();


    So the reason for the crash could possible have the same source, I think.
     
    dyuldashev likes this.
  9. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    74
    I am not saying that the Reset() function is called when switching from an AR scene to a non-AR scene. However, as @ziemlich3D pointed out, these issues are likely to be connected. I mean it should not be too hard to come to that conclusion.

    When you call the Reset() function, what are you actually doing? It's like resetting a simple Unity scene. You destroy everything and you run things again from the beginning.
    When you switch from an AR scene to any other scene, you destroy very similar components.

    The problem starts when you open a new AR scene, or after you Reset() the scene. So, when we are destroying AR components for the first time, it's deleting some components, and those components are not retained when you start a new AR Session. That's why, I got the "pointer being freed was not allocated" error, which simply means that we are trying to delete an AR component that was already deleted.

    You could find where things are being deleted, and only delete the Game Objects that are not null, which might temporarily solve these issues. However, when we exit from an AR scene or when we Reset() an AR Session, some AR components are being deleted. And it does take a genius to conclude that now our tracking is compromised as well because some AR components were not retained in the new session. All of these issues are related, in my view...

    Does it make sense?
     
    Last edited: Oct 19, 2018
  10. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Not sure if this info helps here.. but when you exit an ARScene.... ARSession.OnDisable() is called and followed by ARSession.OnDestroy()

    I had some delay on Android when exiting an AR Scene... the profiler at the time is here:

    https://forum.unity.com/threads/sce...on-android-ar-foundation.546438/#post-3746233

    .. you can see they were causing the delay. They stop and cleanup the AR subsystems.. which is probably where the problem lies perhaps? I didnt change anything other than upgrade ARCore on my device itself... and its improved.

    Code (CSharp):
    1.         void OnDisable()
    2.         {
    3.             ARSubsystemManager.StopSubsystems();
    4.         }
    5.  
    6.         void OnDestroy()
    7.         {
    8.             ARSubsystemManager.DestroySubsystems();
    9.         }
     
    dyuldashev likes this.
  11. viktor_chernikov

    viktor_chernikov

    Joined:
    Oct 8, 2018
    Posts:
    10
    In my experience "pointer being freed was not allocated" in Obj-C always indicates an error. Since AR Foundation (or ARKit) don't work with naked pointers the client code (everything which is possible to write in C# on Unity side) should be completely isolated from pointer's lifetime management. I don't know what exactly is causing this crash in your case, but the fix is obvious: check the pointer before deallocating and set it to nil right after. Unfortunately, it must be done on the native side, so we can only walk around this bug.

    In my case Reset() wasn't necessary at all. I guess I used it because I saw it done in an example. But in any case, I should be able to spam Reset() as often as I want and not have a crash. Those incorrect AR flow errors simply unmask underlying bugs that are there.
     
    GreeneMachine likes this.
  12. Lukas_Andriejunas

    Lukas_Andriejunas

    Unity Technologies

    Joined:
    Aug 19, 2016
    Posts:
    17
    rob_ice, andorfy and ziemlich3D like this.
  13. Augmently

    Augmently

    Joined:
    Aug 23, 2017
    Posts:
    1
    It says that the issue is fixed and it isn't and I'm running the latest preview for both arfoundation and arcore xr.
     
  14. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    I also ran into this misbelief at first sight, but if you look more exactly at the affected package it says "ARExtensions", as TOBIMSQ already pointed out at the ticket (was changed from ARFoundation maybe at the point of time, as the programmers identified the lines of code, which cause this issue). So we have to wait for an update of this package, which is, as far as I know, part of Unity itself and not updateable via package (am I right?).
    I already asked Lukas if he can supply us with a rough estimation on the release, but currently waiting for an answer.
     
  15. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    The new preview-packages for ARKit & ARCore & ARFoundation seem to have fixed this issue. I just tested my app on both Android and iOS:
    Reset() seems to work as well as the scene-change from and to the AR-scene multiple times.
    Thank you very much! :)
     
  16. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    hi @ziemlich3D

    that's great to hear.. .Did you have any tracking issues on IOS? Are you disabling the ARPlanemanager?

    Would you mind specifying the following for me:

    Unity version;

    ARFoundation ver:
    ARCore ver:
    ARKit ver:

    xCode ver:
    Targeted IOS ver:
    Test device IOS ver

    Android test device

    Any other info you think is relevant...

    I am stuck on some old versions as ugrading different packages caused different problems.. really want to upgrade to latest combination of stable versions

    Thanks in advance!
     
  17. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Hi GreeneMachine,

    currently I don't have tracking issues on iOS. But I just tested the Reset() and switching of scenes, yet. I didn't do intensive tests on the tracking, but for my short tests it was good. Working currently at another part of the project, so there isn't much time to do tests. Just wanted to see, if it works ;)
    I don't disable the ARPlaneManager, but I'm using a slightly changed version of it to hide and show the tracked planes in my project.
    Currently I'm using Unity 2018.2.14f1 and the latest updates of ARFoundation 20, ARKit 17 & ARCore 23.
    The newest XCode (did the last update on Friday), can't tell you the exact version, as I'm currently on Windows. Target iOS is 11 and the devices iOS is 12.0.1. Its an iPad 2017.
    Android testdevice is a Galaxy S7 with the newest firmware on it. I think it is 8.0.0 with some custom patches by Samsung.

    I didn't have any problems upgrading from some older packages to newer ones, as you wrote. I just install all updates I can get, as they are published :)
     
    rob_ice likes this.
  18. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Thanks @ziemlich3D

    Sounds like the tracking issue on disabling ARPlaneManager should be resolved in ARKit preview 17.. along with some other stuff.

    https://forum.unity.com/threads/arfoundation-release-1-0-0-preview-20.581434/

    Need to update and do a few tests, but like you, I'm also getting pulled by other parts of the project. Let us know how your tests go when you get a chance. I'll follow up here also once I get to do a decent build.

    Thanks
     
  19. levoxtrip

    levoxtrip

    Joined:
    May 4, 2020
    Posts:
    45
    I have the same problem still. When I call arSession.reset() my android app crahsed