Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

System.TypedReference bugged? can't figure out how to use it !!!

Discussion in 'Scripting' started by tiancaiwrk, Oct 18, 2018.

  1. tiancaiwrk

    tiancaiwrk

    Joined:
    Nov 23, 2017
    Posts:
    32
    C# has some way to get Generic Type target pointer, such as GCHandle / Marshal,
    this time I want to use System.TypedReference for getting pointer of any struct, bug I thing it's bugged...
    I dont know how to report a BUG to Unity, just post it here
    here is the code, it works in VS project but not unity project:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         int v1 = 100;
    4.         int v2 = 1000;
    5.  
    6.         var m1 = __makeref(v1);
    7.         var m2 = __makeref(v2);
    8.  
    9.         var val1 = __refvalue(m1, int);     // val1 is 100
    10.         var val2 = __refvalue(m2, int);     // val1 is 1000
    11.  
    12.         Debug.Log(val1);
    13.         Debug.Log(val2);
    14.  
    15.         unsafe
    16.         {
    17.             *(IntPtr*)(&m1) = new IntPtr((void*)&v2);
    18.  
    19.             val1 = __refvalue(m1, int);     // val1 is 100 -- not changed
    20.             val2 = __refvalue(m2, int);     // val2 is 1000
    21.  
    22.             Debug.Log(val1);
    23.             Debug.Log(val2);
    24.  
    25.             var ptr = *(IntPtr*)(&m1);
    26.             var p = (int*)ptr.ToPointer();
    27.             var data = *p;                  // data is 1000 -- changed
    28.  
    29.             Debug.Log(data);
    30.         }
    31.     }
    And what's more, if I dont Set the pointer of m1, the value of [data] is Strange!!

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         int v1 = 100;
    4.         int v2 = 1000;
    5.  
    6.         var m1 = __makeref(v1);
    7.         var m2 = __makeref(v2);
    8.  
    9.         var val1 = __refvalue(m1, int);     // val1 is 100
    10.         var val2 = __refvalue(m2, int);     // val1 is 1000
    11.  
    12.         Debug.Log(val1);
    13.         Debug.Log(val2);
    14.  
    15.         unsafe
    16.         {
    17.             var ptr = *(IntPtr*)(&m1);
    18.             var p = (int*)ptr.ToPointer();
    19.             var data = *p;                  // data is 536019728 [random] -- WTF
    20.  
    21.             Debug.Log(data);
    22.         }
    23.     }
    This issue is happened in unity project, it work as expected in VS project.
     
  2. tiancaiwrk

    tiancaiwrk

    Joined:
    Nov 23, 2017
    Posts:
    32
    the VS project value is :
    Code (CSharp):
    1.  
    2.         static unsafe void TT()
    3.         {
    4.             int v1 = 100;
    5.             int v2 = 1000;
    6.  
    7.             var m1 = __makeref(v1);
    8.             var m2 = __makeref(v2);
    9.  
    10.             var val1 = __refvalue(m1, int);     // val1 is 100
    11.             var val2 = __refvalue(m2, int);     // val2 is 1000
    12.  
    13.             unsafe
    14.             {
    15.                 *(IntPtr*)(&m1) = new IntPtr((void*)&v2);
    16.  
    17.                 val1 = __refvalue(m1, int);     // val1 is 1000
    18.                 val2 = __refvalue(m2, int);     // val2 is 1000
    19.  
    20.                 var ptr = *(IntPtr*)(&m1);
    21.                 var p = (int*)ptr.ToPointer();
    22.                 var data = *p;                  // data is 1000
    23.             }
    24.         }
     
  3. tiancaiwrk

    tiancaiwrk

    Joined:
    Nov 23, 2017
    Posts:
    32
    Perfect
    Code (CSharp):
    1.         static unsafe void TT()
    2.         {
    3.             int v1 = 100;
    4.             int v2 = 1000;
    5.  
    6.             var m1 = __makeref(v1);
    7.             var m2 = __makeref(v2);
    8.  
    9.             var val1 = __refvalue(m1, int);     // val1 is 100
    10.             var val2 = __refvalue(m2, int);     // val2 is 1000
    11.  
    12.             unsafe
    13.             {
    14.                 var ptr = *(IntPtr*)(&m1);
    15.                 var p = (int*)ptr.ToPointer();
    16.                 var data = *p;                  // data is 100
    17.             }
    18.         }