Search Unity

Unity Player crash when calling method on covariant generic interface C#

Discussion in 'Editor & General Support' started by Khanable, Jan 23, 2014.

  1. Khanable

    Khanable

    Joined:
    Jan 23, 2014
    Posts:
    2
    Hi,

    I'm having an issue where unity crashes when methods on covariant generic interfaces are called. I've comment out any work performed by the methods to confirm it is defiantly the act of calling the methods that causes the crash. Here is a snippet from the crash error log:

    Unity Player [version: Unity 4.3.3f1_c8ca9b6b9936]

    mono.dll caused an Access Violation (0xc0000005)
    in module mono.dll at 0023:100010c6.
    ....
    ....
    Read from location 00000000 caused an access violation.

    I've searched around the web for similar issues but couldn't find anything that seemed related. (same result different cause).
    As far as I can tell (correct me if I'm wrong) variance in generic interfaces should work with mono in the current version of unity (with the target framework set at .net 4.0).

    Here is a snippet of the way I've implement the interface in case I'm the cause of the problem.... (variable names changed/code cuts for clarity)

    Code (csharp):
    1. //Interface
    2. public interface ICovariant<out T> {
    3.  
    4.     void TestMethod();
    5. }
    6.  
    7. //Implemented on Class
    8. public class TestClass<T> :  ICovariant<T> where T : BaseType {
    9.  
    10.     public void TestMethod() {
    11.         int x = 1;
    12.     }
    13.  
    14. }
    15.  
    16. //Called from GameObject Component
    17. public class BaseType : GameBase {
    18.  
    19.     void Start() {
    20.    
    21.          ICovariant<BaseType> MethodObject = new TestClass<DerivedType>();
    22.         MethodObject.TestMethod();
    23.  
    24.     }
    25.  
    26. }
    Is the problem with Unity or myself?
    Let me know if you need any further information and thanks for the help in advance. :D
     
    Last edited: Jan 23, 2014
  2. ViSoft

    ViSoft

    Joined:
    Jan 9, 2014
    Posts:
    1
    Similar problem!
     
  3. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Unity currently targets .NET 3.5, not .NET 4.

    It should be noticed though that if you have:

    Code (csharp):
    1.  
    2. var baseList = new List<BaseType>();
    3.  
    And you want to add derived types to that list you can...

    Code (csharp):
    1.  
    2. public class DerivedType : BaseType
    3. {
    4.      ////
    5. }
    6.  

    Code (csharp):
    1.  
    2.  
    3. baseList.Add(new DerivedType());
    4.  
    5.  
    Then when getting the items back out you can do something like:

    Code (csharp):
    1.  
    2.  
    3. foreach(var itm in baseList)
    4. {
    5.      if(itm is DerivedType)
    6.      {
    7.              ((DerivedType)itm).SomeMethodInDerivedType();
    8.      }
    9. }
    10.  
     
    Last edited: Jan 27, 2014
  4. Khanable

    Khanable

    Joined:
    Jan 23, 2014
    Posts:
    2
    Thanks for the clarification,
    This is the direction I ended up going with.
     
  5. arturaz

    arturaz

    Joined:
    Mar 28, 2013
    Posts:
    30
    Yup. I can confirm that. Reported to bugtracker as well.
     
  6. mikesmullin

    mikesmullin

    Joined:
    Apr 13, 2015
    Posts:
    1
    i ran into this also but i couldn't simplify down to `new List<Base>().Add(Derived)`. I needed a `List<Action<Base>>` but this requires variants if you want to `.Add(Action<Derived>)`. after thinking about it for several hours i was able to figure out a way to get it to work using `List<Action<Type, Base>>` plus lambda scope and a cast such as `.Add((Type t, Base b) => { if (t == typeof(T)) { action.Invoke((T) b); }}`. i thought it was a pretty clever alternative to variants that works with Unity. i haven't explored it all the way but i suspect it can probably do whatever variants can do, and without using reflection.