Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

ArrayTypeMismatchException - Covariance Mono bug

Discussion in 'Scripting' started by Arthur-LVGameDev, Apr 9, 2020.

  1. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    Hi,

    We've got some code that looks good to me but is causing intermittent ArrayTypeMismatchExceptions at runtime -- appears related to (likely the same) as this specific Mono bug (#6266) -- and after checking the code, looks like the fix isn't contained in Unity's Mono fork. Another repro of the ~ same thing can be found on the Mono bug report (see commit message on linked GH commit that fixes #6266 above).

    Repro Specs
    Unity Editor 2019.2.21f1 on macOS
    Scripting Backend: Mono
    Scripting API: .NET 4.x

    Code to Reproduce
    Note, it's intermittent; run RunTest() 20 times or so and you should see at least 1 exception, and possibly 15-20 even.

    Code (CSharp):
    1.  
    2. public interface ITypical {}
    3. public class StdClass : ITypical {}
    4. public class MonoClass : MonoBehaviour, ITypical { }
    5. public class ContainerClass {
    6.     public GameObject go;
    7.     public StdClass nonMono;
    8. }
    9.  
    10. public class MiscClass {
    11.     static List<ITypical> list = new List<ITypical>();
    12.  
    13.     public void RunTest() {
    14.         var test = new ContainerClass() {
    15.             go = new GameObject().AddComponent<MonoClass>().gameObject,
    16.             nonMono = new StdClass()
    17.         };
    18.  
    19.         Test(test);
    20.     }
    21.  
    22.     private List<ITypical> Test(ContainerClass container) {
    23.         list.Clear();
    24.  
    25.         if (container.go != null)
    26.             container.go.GetComponents<ITypical>(list);
    27.  
    28.         if (container.nonMono != null && container.nonMono is ITypical itypInstance) {
    29.             // All 3 variants will intermittently result in runtime errors;  it always compiles, and is (AFAICT) impossible for a TypeMismatch to *actually* occur.  Stack will point to these lines.
    30.             list.Add(container.nonMono);                // result = intermittent runtime error
    31.             //list.Add(itypInstance);                    // result = intermittent runtime error
    32.             //list.Add(container.nonMono as ITypical);    // result = intermittent runtime error
    33.         }
    34.  
    35.         return list;
    36.     }
    37. }
    38.  
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,340
    Did you report a bug?
     
  3. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    I did indeed just now report a bug, yes. :)

    Case # is: 1235903