[CRASH] Loading BinaryFormatter Serialized Data

If you serialize data using binary formatter on unity 2022.1a11 or older and try to load into 2022.1a12 it crashes instantly.
Same happens with 2021.2b15 into 2021.2b16.
I believe its cause of this:

Follows the stack trace of the crash:

========== OUTPUTTING STACK TRACE ==================

0x00007FF961605426 (GameAssembly) [C:\Program Files\Unity\Hub\Editor\2022.1.0a12\Editor\Data\il2cpp\libil2cpp\metadata\Il2CppTypeHash.cpp:21] il2cpp::metadata::Il2CppTypeHash::Hash
0x00007FF96160E326 (GameAssembly) [C:\Program Files\Unity\Hub\Editor\2022.1.0a12\Editor\Data\il2cpp\libil2cpp\gc\AppendOnlyGCHashMap.h:85] il2cpp::gc::AppendOnlyGCHashMap<Il2CppType const *,Il2CppReflectionType *,il2cpp::metadata::Il2CppTypeHash,il2cpp::metadata::Il2CppTypeEqualityComparer>::TryGetValue
0x00007FF96160BB60 (GameAssembly) [C:\Program Files\Unity\Hub\Editor\2022.1.0a12\Editor\Data\il2cpp\libil2cpp\vm\Reflection.cpp:320] il2cpp::vm::Reflection::GetTypeObject
0x00007FF961EE121A (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Generics9.cpp:20660] Dictionary_2_OnDeserialization_mADB79A17862C2B25B6685D7C675FDD78AE8F14CC_gshared
0x00007FF96140B143 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Assembly-CSharp6.cpp:27175] OnSuccess_Invoke_m312C6E2A6A55109600D1EFF83CF29AA5F9A5C96A_Multicast
0x00007FF961FEB28F (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib16.cpp:8763] ObjectReader_Deserialize_mBBC8EE53F2338F334951C3F4343E33BB1278F2E1
0x00007FF961FC85E8 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib15.cpp:28899] BinaryFormatter_Deserialize_mFC50E75F230189EB3660C8F2FD1E786C0D11E1ED
0x00007FF9619BA81C (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\GenericMethods13.cpp:32901] Helper_DeserializeFile_TisRuntimeObject_m0AF2CE71F6099EF374AB2AD1CF560D34313F4601_gshared
0x00007FF9618E01A9 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Assembly-CSharp9.cpp:11234] Customization_LoadPlayerData_m4A9CF9E0B0FA79E9E8018B990B730B6844C967B8
0x00007FF96181F885 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Assembly-CSharp24.cpp:34577] Manager_Init_mB47906DA561B96B3F597387EF49A87CD763532DB
0x00007FF9618EBABE (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Assembly-CSharp9.cpp:28713] Helper_DatabaseDeserialize_mB5AD13FF7FE4906FECDB0BAC9D11AD6767B6E973
0x00007FF961B0F186 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Generics.cpp:29359] U3CGetAssetU3Ed__57_1_MoveNext_mEABF1F371A79A22BB021B5804B4700A1C6B4A659_gshared
0x00007FF9621625C5 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib8.cpp:23970] ExecutionContext_RunInternal_mFA922C000D78E36A9A994FFEA9493DA20240BB64
0x00007FF96200B768 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib17.cpp:11195] MoveNextRunner_Run_m86395F123FB453F2B5FF89CE1E98532F7426F9DE
0x00007FF962174491 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib9.cpp:18064] AwaitTaskContinuation_RunCallback_m37C4A227BA403A3D28CC94369A8770F1C16AE4E5
0x00007FF9621839AB (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib9.cpp:17342] SynchronizationContextAwaitTaskContinuation_Run_mE56D2F76E570D6E0DA92816F6121B3F478219B7D
0x00007FF96218998C (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib9.cpp:13667] Task_FinishContinuations_m21B540B556CDABD76F5985DF83079F7F30C78174
0x00007FF961DB4292 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Generics48.cpp:9877] Task_1_TrySetResult_m80576355963E201DD03E813734ED4CDA847E199E_gshared
0x00007FF961DF9EBD (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Generics5.cpp:21255] AsyncTaskMethodBuilder_1_SetResult_m46806C460117058C2FEC2AC46948F213CF5B4D11_gshared
0x00007FF96170BEDE (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\Assembly-CSharp14.cpp:17423] U3CAwaitU3Ed__2_MoveNext_m9A2452C9B097597A5F58C8EE0AF79CED68C479A0
0x00007FF9621625C5 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib8.cpp:23970] ExecutionContext_RunInternal_mFA922C000D78E36A9A994FFEA9493DA20240BB64
0x00007FF96200B768 (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\mscorlib17.cpp:11195] MoveNextRunner_Run_m86395F123FB453F2B5FF89CE1E98532F7426F9DE
0x00007FF962508CAB (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\UnityEngine.CoreModule1.cpp:23165] WorkRequest_Invoke_mBFEC6A3799BAFDE7ED840A0523D3D56160C03921
0x00007FF962505F5C (GameAssembly) [C:\dev\src\YGOClient\Library\Bee\artifacts\WinPlayerBuildProgram\il2cppOutput\cpp\UnityEngine.CoreModule1.cpp:22843] UnitySynchronizationContext_Exec_m7E6352CBA70E4AB14F7D50A919C3F8C22F2E977A
0x00007FF96157F3C6 (GameAssembly) [C:\Program Files\Unity\Hub\Editor\2022.1.0a12\Editor\Data\il2cpp\libil2cpp\vm\Runtime.cpp:577] il2cpp::vm::Runtime::InvokeWithThrow
0x00007FF96157EE69 (GameAssembly) [C:\Program Files\Unity\Hub\Editor\2022.1.0a12\Editor\Data\il2cpp\libil2cpp\vm\Runtime.cpp:562] il2cpp::vm::Runtime::Invoke
1 Like

I agree this is a big issue. We updated to the latest version of Unity. Every user who has a save file in the old format, will not be able to read that save. That means, users will lose their already saved data. How can it be that Unity doesn’t have a way to read legacy format?

1 Like

I second this, there should either be a way to load legacy format data or a way to convert already existing legacy data to a modern format.

1 Like

+1

This is a bigggg issue for my project which uses legacy data.

1 Like

Hi !
What I’m about to talk about has nothing to do with the version of Unity discussed by OP.
However it seems relevant.

tl;dr => DON’T use BinaryFormatter, ever. And if you do, steer away from it.

I had to investigate issues related to BinaryFormatter last week. It was not a crash, but rather some deserialized values being null (in 2020.3), where they weren’t in a previous unity version (5.4.1).
The takeaway from my researches are:

  • BinaryFormatter is a security breach in your program and as such is considered obsolete and deprecated. Applications should stop using BinaryFormatter as soon as possible.
  • BinaryFormatter format seems to change regularly, and thus is unreliable for data storage (this could be the reason why you experience a crash, since Unity could be making progress in regards to the dotnet api version).
  • It seems it could marked for removal (not sure though).

Here’s two useful links where you could find more information:

So, in conclusion, know that I feel your pain. I have been able to find a hacky workaround for my own issue but it seems different for you there.
However, even if Unity provides some sort of fix for this, I strongly advise you to steer away from this tool as fast as possible. We decided to do so and only fixed it for backward compatibility reasons, and from now on our saves will use a different format (json + some sort of encryption).

In hope this can help

1 Like