- Home /
Serialization: Marshal.StructureToPtr does not work "always"
Hi.
I am using unity 2017.2.0f3. In the editor i have no issues whatsoever. When I started testing a build on android though it didnt work on 1 structure. It still works perfectly well on all other structs i am using. but the app crashes and produces no output from logcat.
The struct in question is:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode, Pack=1)]
[Serializable]
public struct Stage{
public int stageNumber;
public int realScore;
[MarshalAs(UnmanagedType.Bool)]
public bool quits;
public int stars;
public float timeSpent;
[MarshalAs(UnmanagedType.Bool)]
public bool isMixed;
[MarshalAs(UnmanagedType.Bool)]
public bool isTest;
[MarshalAs(UnmanagedType.Bool)]
public bool needstoUpload;
public int associatedSessionNumber;
public int numberOfMoves;
public int stageCount;
[MarshalAs(UnmanagedType.Bool)]
public bool win;
public UInt32 result;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string wordCategory;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
public string profileId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public int[] score;
}|
The serialization that works on editor and on device for the other structs is:
public static byte[] RawSerialize(object anything)
{
Debug.Log("a1");
int rawSize = Marshal.SizeOf(anything);
Debug.Log("a2");
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
Debug.Log("a3");
Marshal.StructureToPtr(anything, buffer, false);
Debug.Log("a4");
byte[] rawDatas = new byte[rawSize];
Debug.Log("a5");
Marshal.Copy(buffer, rawDatas, 0, rawSize);
Debug.Log("a6");
Marshal.FreeHGlobal(buffer);
Debug.Log("a7");
return rawDatas;
}
Pardon the debug logs, but since logcat had no output on the crash I had to go this way "a3" is the last output when i serialize the stage struct. All other structs have basically the same structure and produce no problems. They have array definitions and other [...] statements with the same char encodings, packing bytes and types.
If any wizard can provide some light to the situation, I would be grateful.
$$anonymous$$anaged to fix it by using IL2CPP.. I managed to figure out that the problem was a SIGSEGV but I have no idea why it happens on that specific struct and not on the others. So i ll leave the question open for the time
Your answer
Follow this Question
Related Questions
How to save ComputeBuffer? 0 Answers
Stuck on splash screen on Android only 0 Answers
Is it possible to serialize and store custom structs on the iPhone in scriptableObjects? 2 Answers
Problem to add a delete method to a saveload script C# 3 Answers
Serializing to disk sometimes fails on Android, but on iOS works fine 1 Answer