Search Unity

Bug Nested SerializeReference in a wrapper, freezes and crashes the editor.

Discussion in 'Editor & General Support' started by RecursiveEclipse, Feb 27, 2021.

  1. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Just posting in case someone encounters something like this on 2020.2+. Bug report number: 1317913


    Minimal example, just add an "AWrapper" to a monobehaviour:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. //Testing interfaces with SerializeReference
    7. public interface IA {}
    8. public interface IB {}
    9.  
    10.  
    11. // ======================== //
    12. //Concrete implementations
    13. //Appending to BWrappers in the inspector will freeze the editor, it will then consume all memory until crash.
    14. [Serializable]
    15. public class AImpl : IA {
    16.     public List<BWrapper> BWrappers;
    17. }
    18.  
    19. [Serializable]
    20. public class BImpl : IB {}
    21. // ======================== //
    22.  
    23.  
    24. // ======================== //
    25. //Wrappers
    26. [Serializable]
    27. public class AWrapper { // hierarchy starts with an instance of this
    28.     [SerializeReference]
    29.     public IA IAImpl = new AImpl();
    30. }
    31. [Serializable]
    32. public class BWrapper {
    33.     [SerializeReference]
    34.     public IB IBImpl;
    35. }
    36. // ======================== //
    37.  
    38.  
    39. // ======================== //
    40. /* SEMI WORKING EXAMPLES
    41. // Initially works, but if BWrappers length becomes 0 in editor and is appended again, causes the freeze.
    42. [Serializable]
    43. public class AImpl : IA {
    44.     public List<BWrapper> BWrappers;
    45.  
    46.     public AImpl() {
    47.         BWrappers = new List<BWrapper> {
    48.             new BWrapper {
    49.                 IBImpl = new BImpl()
    50.             }
    51.         };
    52.     }
    53. }
    54.  
    55. // Initially works as above, but if appended to, BWrapper constructors are not called.
    56. [Serializable]
    57. public class AImpl : IA {
    58.     [SerializeReference]
    59.     public List<BWrapper> BWrappers;
    60.  
    61.     public AImpl() {
    62.         BWrappers = new List<BWrapper> {
    63.             new BWrapper {
    64.                 IBImpl = new BImpl()
    65.             }
    66.         };
    67.     }
    68. }
    69. */
    70. // ======================== //
    71.  
    72.