iOS going to background crashes at ResetOwnership with CurrentRoom == null

Hi there!

I have almost completed my game with Unity 2019.4.6f1 and PUN2 v2.19.3 . Currently I am struggling with iOS, where going to background cause game crash by PUN2. I wait in background (about 10 seconds) until server disconnects me, after that I return back from background and iOS game crashes immediately. It crashes at PhotonView at ResetOwnership at SetOwnerInternal(PhotonNetwork.CurrentRoom.GetPlayer(this.CreatorActorNr), CreatorActorNr), where CurrentRoom is null.
I have also checked call stack and it looks like that on OnDisconnect PUN2 set CurrentRoom to null and after that PUN2 call ResetOwnership .
Is there any workaround for this case?

Also Unity usually raise errors inside unity player on iOS without crashes. It is very strange that PUN2 cause crashes.

Thank you!

Comments

  • ArchiArchi
    edited August 2
    I have made a workaround to prevent crashing by adding 1 line of code. But I don't know is it correct?

    Here the line I have added:
    if (PhotonNetwork.CurrentRoom != null)
    

    And here is how ResetOwnership() looks now:
    internal void ResetOwnership()
            {
                if (CreatorActorNr == 0)
                    SetOwnerInternal(null, 0);
                else
                {
                    if (PhotonNetwork.CurrentRoom != null)
                    {
                        SetOwnerInternal(PhotonNetwork.CurrentRoom.GetPlayer(this.CreatorActorNr), CreatorActorNr);
                    }
                    else
                    {
                        Debug.LogError("PhotonView.ResetOwnership with PhotonNetwork.CurrentRoom == null");
                    }
                }
            }
    
  • ArchiArchi
    edited August 2
    Also I wanted to add that I am not using any Ownership methods in my project. My game has two player. Scene instate (Unity Instate) 3 prefabs (each Prefab has it's own PhotonView). When 2 players are ready, 1st player call bindGameObjectNetRise for the 1st prefab and 2nd player call bindGameObjectNetRise for 2nd and 3rd prefab too. The whole game works fine but if disconnect by server in background occurs the game will crash. Below are my bindGameObjectNetRise code and bindGameObjectNetEvent - event handler (when game receives the event).

    bindGameObjectNetRise:
    void bindGameObjectNetRise(byte gameObjectId)
        {
            PhotonView photonView = null;
            IdMonoBehaviour bindGameObject = idMonoBehaviours[gameObjectId];
            if (bindGameObject != null)
            {
                photonView = bindGameObject.GetComponent<PhotonView>();
            }
    
            if (photonView != null && PhotonNetwork.AllocateViewID(photonView))
            {
                object[] data = new object[]
                {
                    bindGameObjectNetCode,
                    gameObjectId,
                    photonView.ViewID
                };
    
                NetworkEventHelper.defaultRaise(NetworkEventCode.mainSceneController, data, ReceiverGroup.Others);
            }
            else
            {
                Debug.LogError("Photon Failed to allocate a ViewId");
            }
        }
    

    bindGameObjectNetEvent:
    void bindGameObjectNetEvent(byte gameObjectId, int viewId)
        {
            PhotonView photonView = null;
            IdMonoBehaviour bindGameObject = idMonoBehaviours[gameObjectId];
            if (bindGameObject != null)
            {
                photonView = bindGameObject.GetComponent<PhotonView>();
            }
            if (photonView != null)
            {
                photonView.ViewID = viewId;
                Debug.Log("Photon bind ViewId");
            }
            else
            {
                Debug.LogError("Photon Failed to bind a ViewId");
            }
        }
    

  • ArchiArchi
    edited August 2
    After some research I found out that manual call PhotonNetwork.Disconnect() in the middle of the game will lead too to Debug.LogError("PhotonView.ResetOwnership with PhotonNetwork.CurrentRoom == null") (see my 1st comment).
  • TobiasTobias admin
    edited August 3
    We noticed the possible nullreference issue, too and got a fix. It's pretty much what you got, too.
    I will push for an update of PUN asap. At least the fixes we did should get released.

    Thanks for the comprehensive post and info. Also for the fix you applied. Much appreciated.
  • This is fixed in PUN 2.20.1 now in the Asset Store.
Sign In or Register to comment.