0

First of all, I would say that, I'm not stuck, as there is a lot of workaround for me !! The best of then is that I don't need 1Byte Pack anymore :P

But honestly, I'm quite puzzle about the behavior. I'm using Unity, It works perfectly on Windows but failed on Android(mono)

  • Have I write something invalid ?
  • Do I have rise a bug on Mono ?!

Comments are definitivly welcome !!

public class TEST
{
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct MyStruct {
        public float   field1;
        public float   field2;
    };

    //This order sucess on windows, but failed on android
    private byte oneByte = 0x00;
    private MyStruct mMyStruct;


    public bool doTest()
    {
        try {
            mMyStruct.field1  = (oneByte++);
        } catch {
            // System.NullReferenceException: Object reference not set to an instance of an object
            return false;
        }
        return true;
    }           
}

Some extras infos/clues:

  • Difference between Android/Windows probably comes from processor architecture difference Android ARM(RISC), suppose 4Bytes alignement (or at least 2Bytes in thumb mode)

  • [StructLayout(LayoutKind.Sequential, Pack = 1)] seems to affect more than the struct.

Memory alignment probably becomes:

[ onByte    ]  [ field1 #0 ] [ field1 #1 ] [ field1 #2 ]
[ field1 #3 ]  [ field2 #1 ] [ field2 #1 ] [ field2 #3 ]
[ field2 #4 ]  [ *pad*     ] [ *pad*     ] [ *pad*     ]

Where I was expected more something like this:

[ onByte    ] [ *pad*     ] [ *pad*     ] [ *pad*     ]
[ field1 #0 ] [ field1 #1 ] [ field1 #2 ] [ field1 #3 ]
[ field2 #1 ] [ field2 #1 ] [ field2 #3 ][ field2 #4 ] 
Steven
  • 166,672
  • 24
  • 332
  • 435
Jan
  • 91
  • 4
  • 1
    Looks like a Mono bug to me. `mMyStruct` can't be null as you rightly surmised (it's a value type). – Matthew Watson Mar 07 '14 at 14:36
  • Using a Pack of 1 is very rarely correct. Since it doesn't matter at all for this struct, the floats are always aligned for any Pack value, just remove it. File a bug with the Mono project to let them know. – Hans Passant Mar 07 '14 at 15:04
  • 1
    Miguel de Icaza, Mono auhtor comment: http://stackoverflow.com/a/16877481/3314372 – Jan Mar 07 '14 at 15:44
  • possible duplicate of [Monotouch crashes with NullReferenceException on non nullable object](http://stackoverflow.com/questions/16203847/monotouch-crashes-with-nullreferenceexception-on-non-nullable-object) – Felix K. Mar 07 '14 at 16:16
  • Had the same bug, remove Pack=1 and its going to work again. Still its not fine. – Felix K. Mar 07 '14 at 16:19

0 Answers0