Search Unity

Application.Unload doesn't work as expected

Discussion in 'Android' started by apichukov, Aug 3, 2018.

  1. apichukov

    apichukov

    Joined:
    Apr 16, 2018
    Posts:
    4
    I have an Android application that can open Unity game. To make passible game close without killing the app itself I use
    Application.Unload
    method in Unity game for unload the Unity runtime. It works sometimes and every time I see this error message in AndroidStudio logs:


    android.app.ServiceConnectionLeaked: Activity com.example.unity.UnityPlayerActivity has leaked ServiceConnection null that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1399)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1294)
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1503)
    at android.app.ContextImpl.bindService(ContextImpl.java:1475)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:688)
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    at com.unity3d.player.UnityPlayer.c(Unknown Source)
    at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source)
    at android.os.MessageQueue.next(MessageQueue.java:392)
    at android.os.Looper.loop(Looper.java:136)
    at com.unity3d.player.UnityPlayer$e.run(Unknown Source)


    I don't have any binded service in my Android application, so looks like it something from Unity side. And the main problem that sometimes after game closing my application crashes without any message.

    Do anyone has the same issue or maybe knows how to fix it? I think I'm doing something wrong with
    Application.Unload
    , but I'm using it according the documentation, just call this function when I need to close the game. Maybe I have to do something else on Unity side ?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How does the game behave when not launched from your app?
     
  3. apichukov

    apichukov

    Joined:
    Apr 16, 2018
    Posts:
    4
    Hey JeffDUnity3D, I don't really understand the question, do you mean with the same Application.Unload operation but as a separate process ? If yes, that it's incorrect to use it like this, because Unload should be used only for unload Unity runtime if game is a part of other application, for separate process I was using Application.Quit and it was working without any issue.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You mention "I have an Android application that can open Unity game". What happens if you launch the Unity game directly, and not from your Android application that is launching it. Perhaps you just mean you have a single Unity game running on Android? It sounds like you have a separate application that then launches another application (the Unity game)
     
  5. apichukov

    apichukov

    Joined:
    Apr 16, 2018
    Posts:
    4
    I have an Android application and one of the things that this application does is to open Unity game, so I try to open the game from this Android application and close it (the game). When I close the game with using Application.Unload method from Unity side, it kills the UnityPlayerActivity and goes back to my app, and I can open game again. But sometimes after a lot of actions like this(open game, close it with going back to app, open it again ...) game crashes and it crash Android app too. If I will run game in Android without application it works the same as inside the app, but without crash on closing it, because there is nothing to crash, it just kills the process and close the game.
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,905
    Which Unity version is this ? Unloading everything in Unity is a bit tricky, so depending on your project, we might left an error somewhere.

    A bug report would be nice.
     
    Yury-Habets likes this.
  7. apichukov

    apichukov

    Joined:
    Apr 16, 2018
    Posts:
    4
    Hey Tomas1856, thanks for reply. I use Unity 2018.2.0f2. And my project is super simple, I did it just for test this Unload possibility. It's just 3 objects and function that I call from native Android app using UnitySendMessage. Here is the code of object that I use to call function from native code and function itself:

    Code (CSharp):
    1. public class NativeFunctions : MonoBehaviour {
    2.  
    3.     void close() {
    4.         #if (UNITY_ANDROID || UNITY_IPHONE)
    5.             Application.Unload();
    6.         #endif
    7.         Destroy(this);
    8.     }
    9. }
     
  8. NinjaCat

    NinjaCat

    R&D Minion

    Joined:
    Aug 22, 2013
    Posts:
    69
    Could you also please tell us which scripting runtime version are you running on? Is it 4.x or 3.5? There are some known issues with the new scripting runtime.
     
  9. GemmaLamont

    GemmaLamont

    Joined:
    Oct 9, 2018
    Posts:
    8
    Hi @NinjaCat, I have the same problem and am using 3.5 :)
     
  10. NinjaCat

    NinjaCat

    R&D Minion

    Joined:
    Aug 22, 2013
    Posts:
    69
    And which Unity version are you on?
     
  11. GemmaLamont

    GemmaLamont

    Joined:
    Oct 9, 2018
    Posts:
    8
    I am using a beta build 2018.3.0b4 currently because I wanted a feature from it but the same was happening on the 2018.2.7f1 version I was using before
     
    Last edited: Oct 16, 2018
  12. NinjaCat

    NinjaCat

    R&D Minion

    Joined:
    Aug 22, 2013
    Posts:
    69
    That should not be happening. Could you please send us a bug report?
     
    GemmaLamont likes this.
  13. GemmaLamontPerigee

    GemmaLamontPerigee

    Joined:
    Sep 14, 2018
    Posts:
    9
    @NinjaCat Any suggestions for how to properly handle this? Or a workaround in the meantime? (I have submitted a bug report now too) (p.s I see I have switched accounts on this one thread! I am the same person as the previous GemmaLamont post :) )
     
    Last edited: Oct 17, 2018
  14. NinjaCat

    NinjaCat

    R&D Minion

    Joined:
    Aug 22, 2013
    Posts:
    69
    No, can't think of anything at the moment. However, we're working to improve this.
     
    GemmaLamont likes this.
  15. GemmaLamontPerigee

    GemmaLamontPerigee

    Joined:
    Sep 14, 2018
    Posts:
    9
    I have found that the service connection leak error wasn't actually caused by this :) However, a crash is still occurring on some devices, the 2nd or 3rd time of restarting the UnityPlayer. I am wondering if it with how I use the Application.Unload() is there a correct way of using it? and if so what is it? I couldn't find any clear documentation around it @NinjaCat :)
     
  16. NinjaCat

    NinjaCat

    R&D Minion

    Joined:
    Aug 22, 2013
    Posts:
    69
    Hi! It shouldn't crash. You last report has been used to track and fix the leak error - could you perhaps create a separate report for the crash? Please consider including a project that could be used to recreate the issue. The crash might be happening due to a project-specific setting, having a project would help us identify the problem much faster.
     
    GemmaLamontPerigee likes this.
  17. unity_xrPBQ-2zcfd-DA

    unity_xrPBQ-2zcfd-DA

    Joined:
    Jun 15, 2018
    Posts:
    19
    I am also getting the same error. I also have an android app which on button click launches unity and when pressing the back button it comes back to android part. But everytime on pressing back this error comes up.
     
  18. GemmaLamontPerigee

    GemmaLamontPerigee

    Joined:
    Sep 14, 2018
    Posts:
    9
    Are you doing any calls from your scripts to Java? Because one thing to make sure to do is have the c# AndroidJava calls wrapped in a 'using' statement or use Dispose after :) that stopped the error for me, although if yours is also crashing still then you probably have the same problem as me :/
     
  19. unity_xrPBQ-2zcfd-DA

    unity_xrPBQ-2zcfd-DA

    Joined:
    Jun 15, 2018
    Posts:
    19
    I am calling the UnitySendMessage function from native to call C# script methods but even if I have simple program i.e just a unity program exported in native android and run that it still gives the same error (not calling any methods).
     
  20. GemmaLamontPerigee

    GemmaLamontPerigee

    Joined:
    Sep 14, 2018
    Posts:
    9
    I meant the other way around so if you are using :

    Code (CSharp):
    1. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
    2. AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")
    then to dispose of them after.
    But if you aren't then maybe create a bug report too :) The more info we give the Unity team the better!
     
  21. unity_xrPBQ-2zcfd-DA

    unity_xrPBQ-2zcfd-DA

    Joined:
    Jun 15, 2018
    Posts:
    19
    Nope I am not calling anything from Unity. Just calling finish() method of android from UnityPlayerActivity on back press.
     
  22. unity_xrPBQ-2zcfd-DA

    unity_xrPBQ-2zcfd-DA

    Joined:
    Jun 15, 2018
    Posts:
    19
  23. GemmaLamont

    GemmaLamont

    Joined:
    Oct 9, 2018
    Posts:
    8
  24. Vash_b

    Vash_b

    Joined:
    Dec 27, 2018
    Posts:
    1
    what irritates me to no end is, i want the exact opposite of what you want right now but i can't get the correct search key for it XDDDDD

    basically i want the activity that calls the unity player activity to close as well, already use the application.Quit but somehow the main activity still remains