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. Dismiss Notice

Unity crashes when adding an element to a list of serialized interfaces

Discussion in 'Editor & General Support' started by sRioni, Apr 27, 2021.

  1. sRioni

    sRioni

    Joined:
    Apr 8, 2020
    Posts:
    12
    I've found what I think is a Unity serializatoin bug while developing my game. Let me explain what happened.

    We have this script that only contains a class definition

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public enum IMyInterfaceSelector { None, CrashType}
    5.  
    6. [Serializable]
    7. public class MyType
    8. {
    9.     public IMyInterfaceSelector selector;
    10.     [SerializeReference]public IMyInterface myInterfaceSerialized;
    11. }
    And we also have a script with the interface and two structs that implements that interface:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public interface IMyInterface
    6. {
    7.     IEnumerator myPropety { get; set; }
    8. }
    9.  
    10. public struct IMyInterface_FistType : IMyInterface
    11. {
    12.     public IEnumerator myPropety { get; set; }
    13. }
    14.  
    15. public struct IMyInterface_SecondType : IMyInterface
    16. {
    17.     [Header("It will crash if you add an element to the list")]
    18.     [SerializeField] private List<MyType> myTypeList;
    19.  
    20.     public IEnumerator myPropety { get; set; }
    21. }
    Alright, now for last, we have a simple script that contains a list of MyType:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class ScriptThatCrashes : MonoBehaviour
    5. {
    6.     [SerializeField] private List<MyType> myTypeList;
    7.  
    8.     private void OnValidate()
    9.     {
    10.         if (myTypeList == null) return;
    11.         for (int i = 0; i < myTypeList.Count; i++)
    12.         {
    13.             switch (myTypeList[i].selector)
    14.             {
    15.                 case IMyInterfaceSelector.None:
    16.                     if (!(myTypeList[i].myInterfaceSerialized is IMyInterface_FistType))
    17.                         myTypeList[i].myInterfaceSerialized = new IMyInterface_FistType();
    18.                     break;
    19.                 case IMyInterfaceSelector.CrashType:
    20.                     if (!(myTypeList[i].myInterfaceSerialized is IMyInterface_SecondType))
    21.                         myTypeList[i].myInterfaceSerialized = new IMyInterface_SecondType();
    22.                     break;
    23.                 default:
    24.                     if (!(myTypeList[i].myInterfaceSerialized is IMyInterface_FistType))
    25.                         myTypeList[i].myInterfaceSerialized = new IMyInterface_FistType();
    26.                     break;
    27.             }
    28.         }
    29.     }
    30. }
    31.  
    Wee need that enum of MyType so we can create instances of IMyInterface in the inspector. Alright, everything works fine, BUT. If we add Crash type to the list in the inspector, and then we try to add an element to the list of the Crash Type (IMyInterface_SeconType). Unity will crash.





    What is more, Unity don't even detect that crash, it takes all the memory possible from the system and freezes these. Check what I got when I get one crash of this:




    I can't even use the task manager, it looks like this, only displaying two apps and memory usage. After a shor time of that:





    Boom, system don't even have memory to render the screens (but somehow, windows don't crash). I had to manually force restart.

    This also happens if I use an array in the "ScriptThatCrashes", also happens if I also use an array in IMyInterface_SecondType (CrashType in the enum). If in that struct I use a single MyType instead of a collection, there is no problem as there is no way to add something to a collection.

    I've already reported it, just wanted to post it here too just in case someone knows something about this.

    Do you want to try it? I don't recommend it, but if you feel like you know what's going, here is a package (check attached files) that contains the scripts and a scene with a GameObject that have the "ScriptThatCrashes". Just have the task manager opened and if Unity starts to crash, try to force close it rapidly, if you can't, you have to manually reboot your system.
     

    Attached Files:

  2. sRioni

    sRioni

    Joined:
    Apr 8, 2020
    Posts:
    12
    Alright, I think I've found what it causes it. I opened the scene file manually and tested a few things to see how serialization works. After that I found that when you add an element to the list, and there is no one, it adds the id: 0, but the id: 0 is the object that contains the list that contains a object that contains a list... It enters a loop, I've checked it in the inspector.



    The above file will make unity crash when you click the gameObject that has the script with that info



    This will work fine
     
    Last edited: Apr 28, 2021
  3. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    @sRioni it looks like a bug. Can you please file a bug report so that QA can look into the issue?
    Ted and I are working on the 2D Animation and in our team we try to reply to every post related to 2D so there is no need for the ping :)
     
  4. sRioni

    sRioni

    Joined:
    Apr 8, 2020
    Posts:
    12
    I filed a bug reported before posting here.
    Oh I see, that makes sense since the entry in which you were mentioned was related to 2D animation, sorry for pinging you, I'll be more careful about that next time and thanks for your kind answer ^^
     
  5. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    329
    I'm having similar problems with the serialized reference attribute
     
  6. Chemical-Code

    Chemical-Code

    Joined:
    Jun 19, 2023
    Posts:
    1
    Unfortunately the bug still persists to this day.