Search Unity

SceneManager causes memory access violation with Singleton...

Discussion in 'Scripting' started by darkForester, Feb 8, 2016.

  1. darkForester

    darkForester

    Joined:
    Sep 19, 2013
    Posts:
    80
    Greetings,

    After toiling with hard crashes on Android, I decided to dig deeper into what has been occurring since that latest Scene Manager updates since 5.3.

    We have a Singleton<T> : MonoBehaviour class that creates itself if it is null; pretty typical stuff.

    However, we were receiving a memory access violation with no further information. So we checked our memory usage on the device, it was fine.

    Our singleton was our "LevelManager" it handled spawning of Coroutines that called and yielded SceneManager.LoadSceneAsync. Our LevelManager, however was NOT set to "DontDestroyOnLoad". So the following situation became evident:

    1. LevelManager was instantiated.
    2. Scene load was initiated via a Coroutine on the LevelManager.
    3. LevelManager was destroyed, leaving a coroutine continuing to execute.
    4. Memory access violation.
    The strange part, is that it only happened on devices (android), never on the PC. This tells me that there is a race condition of sorts between the unmanaged code and managed code.

    Our Solution
    Simply keeping a systems object in a scene with "DontDestroyOnLoad" seemed to make the issue disappear. However we only began having these crashes once Unity changed to using the SceneManager.

    Easy work-around, however something for the dev. team to look into.

    PS: Our project is too large to submit to a bug report. Eventually I can try to reproduce in a smaller test project. But I wanted to get the word out on this situation in case someone else was dealing with it also.

    Thanks for listening!