Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Possible leak

Discussion in 'Cinemachine' started by VariableStateEd, Nov 5, 2019.

  1. VariableStateEd

    VariableStateEd

    Joined:
    Aug 3, 2018
    Posts:
    3
    I'm using the "new" Memory Profiler to check for leaks in a game project. When I use the Memory Profiler in Diff mode, I notice that Cinemachine components always appear in the New category, never in the Deleted category. This suggests that allocations may be being made which are never freed - a leak.

    I have reproduced this in a small test project. I can post this project somewhere if it would be helpful. Here is a description.

    Import the Cinemachine and Memory Profiler packages using the Package Manager.

    Create a new script named MainController as follows:

    Code (CSharp):
    1. public class MainController : MonoBehaviour
    2. {
    3.     private void Awake()
    4.     {
    5.         DontDestroyOnLoad(this.gameObject);
    6.     }
    7.  
    8.     private void Start()
    9.     {
    10.         StartCoroutine(DoTest());
    11.     }
    12.  
    13.     private IEnumerator DoTest()
    14.     {
    15.         while (true)
    16.         {
    17.             SceneManager.LoadScene("Empty", LoadSceneMode.Single);
    18.  
    19.             while (!Input.GetKeyDown(KeyCode.Space))
    20.             {
    21.                 yield return null;
    22.             }
    23.  
    24.             Debug.Log("Running test...");
    25.  
    26.             SceneManager.LoadScene("ActiveCamera", LoadSceneMode.Single);
    27.  
    28.             yield return new WaitForSeconds(3.0f);
    29.  
    30.             Debug.Log("Test complete");
    31.         }
    32.     }
    33. }
    34.  
    Create a scene named BootScene, with a single GameObject which has a Main Controller, a Cinemachine Brain and a Camera.
    Create another completely empty scene named "Empty".
    Create a third scene named "ActiveCamera", with one or more GameObjects with CinemachineVirtualCameras.
    Add the three scenes to Build Settings.

    Open BootScene in the editor and enter Play mode.
    Press Space to start the test. Wait for "Test complete" to appear in the console.
    Open the Memory Profiler and capture a snapshot.
    Focus the Game window and press Space to run another test. Wait for "Test complete" again. Repeat several times.
    Capture a second snapshot in the Memory Profiler.

    Open the two snapshots in the Memory Profiler and press the Diff button.
    Select the Diff column header and choose Group. Select the Data Type column and choose Group. Select the Type column and Sort Ascending.
    Expand New - Managed Object. Note that there is a Cinemachine.CinemachineVirtualCamera entry for each virtual camera, for each time the test was run.
    Expand Deleted - Managed Object. Note that there are no entries for Cinemachine.

    Unity 2019.1.14f1, Cinemachine 2.3.4, Memory Profiler preview.7 - 0.1.0.

    Am I doing something wrong? Is the Memory Profiler reporting something incorrectly? Is this a genuine leak?

    Cheers, Ed
     
    Last edited: Nov 5, 2019
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Hey Ed, thanks for this post.
    You can DM me the project and I can take a look.
     
  3. VariableStateEd

    VariableStateEd

    Joined:
    Aug 3, 2018
    Posts:
    3
    Thanks! Link sent.
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hi Ed!
    Thank you for reporting the leak, and creating the project to test it! ;)

    It was a genuine leak; only empty C# wrappers got leaked though.
    We have fixed it. It will be available in an upcoming release. :)
     
  5. VariableStateEd

    VariableStateEd

    Joined:
    Aug 3, 2018
    Posts:
    3
    Great, thanks!