问题
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 ]
来源:https://stackoverflow.com/questions/22252819/c-sharp-structlayout-pack-get-system-nullreferenceexception-alignement-issue