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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Closed] Crash when disconnecting airplay external display (Screen Mirroring)

Discussion in 'iOS and tvOS' started by Tony-Tonijn, Mar 25, 2020.

  1. Tony-Tonijn

    Tony-Tonijn

    Joined:
    Nov 13, 2013
    Posts:
    7
    Hey there,

    I am developing a simulation with Unity where we need to use airplay displays to show Unity content on with a different resolution than the one the iPad has (1920x1080). For that we use the example code provided by the Unity documentation on https://docs.unity3d.com/ScriptReference/Display.html.

    Connecting to the display works fine. However, when disconnecting the App crashes with an assertion error:
    Code (CSharp):
    1. *** Assertion failure in -[DisplayConnection dealloc], Classes/Unity/DisplayManager.mm:234
    It seems that the
    Code (CSharp):
    1. _screen
    is not deallocated or set to 0 when a screen disconnect occurs. Since the NSAssert fires in DisplayManager.mm on line 234 in the dealloc of the DisplayConnection class. However, my Objective-c is rusty to non-existent, so I don't know.

    Removing this assertion fixes my issue, but I have no idea how safe that is, or if that results in lingering pointers to some memory used for the screen representations.

    To me this feels like a bug in the code generated by Unity during building. But it might be that I am overlooking something where I have to free some Display buffers? However, I can't seem to find any call which would do the opposite to SetTargetBuffers. Do I need to call it with null? Because I am feeling lost at this moment.

    This is the code I'm using.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MultidisplayExample : MonoBehaviour {
    6.    
    7.     public Camera mainCamera;
    8.     public Camera extCam;
    9.    
    10.     void Start() {
    11.         extCam = GetComponent<Camera>();
    12.         // GUI is rendered with last camera.
    13.         // As we want it to end up in the main screen, make sure main camera is the last one drawn.
    14.         extCam.depth = mainCamera.depth - 1;
    15.         mainCamera.SetTargetBuffers(Display.main.colorBuffer, Display.main.depthBuffer);
    16.  
    17.         extCam.enabled = false;
    18.     }
    19.  
    20.     void Update() {
    21.         if (Display.displays.Length > 1 && !extCam.enabled) {
    22.             Display.displays[1].SetRenderingResolution(1920, 1080);
    23.             extCam.SetTargetBuffers(Display.displays[1].colorBuffer, Display.displays[1].depthBuffer);
    24.         }
    25.         extCam.enabled = Display.displays.Length > 1;
    26.     }
    27. }
    Additional Information :
    • I am using a 2nd generation iPad Pro (2732x4048) on iOS 13.3.1
    • Using Unity 2019.3.2f1
    • I've added the crash log to the thread
     

    Attached Files:

  2. vvvvalentin

    vvvvalentin

    Unity Technologies

    Joined:
    Nov 11, 2015
    Posts:
    4
    Tony-Tonijn likes this.
  3. Tony-Tonijn

    Tony-Tonijn

    Joined:
    Nov 13, 2013
    Posts:
    7
    Hey vvvvalentin,

    Thanks for the heads up. I will close this thread and look forward to the back-port.

    Kind regards and stay safe during this hard period.
     
  4. Tony-Tonijn

    Tony-Tonijn

    Joined:
    Nov 13, 2013
    Posts:
    7
    Dear Vvvvalentin,

    I have looked thoroughly through the latest releases of 2019.3.2f1 up to 2019.3.9f1 and so far I can't seem to find this issue in the 'Known Issues' section nor in the 'Bug Fixes' section. Our timeline will soon start to be effected by this major bug. Can you, or one of your colleagues, give me a timeline on 2019LTS or is there a possibility that this can be included in a patch release / the next 2019.3 release?

    Kind regards, and stay safe.
     
  5. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    Hi.

    The fix is in Unity 2019.3.13f1 which will be available in the upcoming weeks.
     
    DebugLogError likes this.
  6. Tony-Tonijn

    Tony-Tonijn

    Joined:
    Nov 13, 2013
    Posts:
    7
    Hey Valyard,

    Thank you for the heads up. It is good to hear that it is being actively worked on. Unfortunately we have had to move back to 2018, since we were unable to wait for the fix due to time constraints in the project.

    Kind regards and stay safe.
     
  7. DebugLogError

    DebugLogError

    Joined:
    Jul 24, 2013
    Posts:
    54
    2019.3.13f1 fixed my iOS app crashing when an external display is disconnected. However, when I reconnect the display I get a black screen (on the external display). Any ideas?

    Code (CSharp):
    1. private void Awake ()
    2.         {
    3.             // Setup cameras
    4.  
    5.             this.CameraInternal.enabled = true;
    6.             this.CameraInternal.depth = 1;
    7.  
    8.             this.CameraExternal.enabled = false;
    9.             this.CameraExternal.depth = 0;
    10.  
    11.             // Setup canvases
    12.  
    13.             this.CanvasInternal.targetDisplay = 0;
    14.             this.CanvasExternal.targetDisplay = 1;
    15.  
    16.             this.CanvasInternal.worldCamera = this.CameraInternal;
    17.             this.CanvasExternal.worldCamera = this.CameraExternal;
    18.  
    19.             this.CameraInternal.SetTargetBuffers (UnityEngine.Display.main.colorBuffer, UnityEngine.Display.main.depthBuffer);
    20.         }
    21.  
    22.         private void Update ()
    23.         {
    24.             if (UnityEngine.Display.displays.Length > 1)
    25.             {
    26.                 if (this.CameraExternal.enabled == false)
    27.                 {
    28.                     UnityEngine.Debug.Log ("External display detected");
    29.  
    30.                     this.ActivateExternalDisplay ();
    31.                 }
    32.             }
    33.             else if (this.CameraExternal.enabled == true)
    34.             {
    35.                 UnityEngine.Debug.Log ("External display disconnected");
    36.  
    37.                 this.CameraExternal.enabled = false;
    38.             }
    39.         }
    40.  
    41.         private void ActivateExternalDisplay()
    42.         {
    43.             if (UnityEngine.Display.displays.Length > 1)
    44.             {
    45.                 UnityEngine.Display externalDisplay = UnityEngine.Display.displays[1];
    46.  
    47.                 externalDisplay.Activate ();
    48.  
    49.                 // Set the rendering resolution for the external display
    50.  
    51.                 externalDisplay.SetRenderingResolution (externalDisplay.systemWidth, externalDisplay.systemHeight);
    52.  
    53.                 // Set the external camera to render to the buffers of the external display
    54.  
    55.                 this.CameraExternal.SetTargetBuffers (externalDisplay.colorBuffer, externalDisplay.depthBuffer);
    56.  
    57.                 this.CameraExternal.enabled = true;
    58.             }
    59.         }