So, long story short, this is obviously valid code. Working in Unity3D- runs fine on PC. Build for Android- NRE when trying to access past the specific address pinned- NRE where commented in the code block below.
public static unsafe byte[] SerialiseFloat(byte header, float val)
{
data = new byte[5];
fixed (byte* b_ptr = data)
{
*b_ptr = header;
*((float*)(b_ptr + 1)) = val; //NRE
return data;
}
}
And not to be offhand but because it seems to come up in every single question about unsafe context C#- yes, I need to be using it. No, it's not that important for this instance, I know it's an unnecessary use of an unsafe context in this example (this is from my old serialisation lib, contracts are generated which hand over to these methods to serialise members, it was just a quick put-together and these things need to be inlined so I'm not working with multiple arrays.
Anyhow, the point stands- NRE at the line commented above. Anybody know how this is even possible, and how I might begin to fix it? It is happening consistently, it's clearly allocated and pinned. Memory analysis shows that it's being referenced absolutely correctly, everything is aligned as expected. Reproduced this about 15 times now.
Thanks to GSerg for his comment pointing me towards this question. I now know of ARM's alignment constraints which come into play when working with floating-point values. I'll simply force my structure packing for strict alignment now that I'm aware of the cause. Thanks for the heads up- being new to mobile dev and ARM I had absolutely no idea about this and would have definitely lost some sleep, so a huge thanks from me!
EDIT: Just an update to confirm that everything is working properly after running through a few serialisation tests with my code generator now preprocessing generated contracts on Android to force floating point fields to always align to 4-byte memory addresses.