I’m using a C++ native plugin with a C# wrapper compiled with swig (which calls functions in the C++ plugin using PInvoke). This is working mostly fine, except that, in some cases, the native code has exceptions or segfaults, which makes my whole game crash.
Of course, I can’t fix the C++ code because it isn’t mine.
I’d like to have a way to run this in a subprocess (not a thread), that way I can gracefully handle a library crash.
Is there any way to do that, something like Linux’s fork(), that would work on both Windows and Android with IL2CPP ?
Process.Start only seems to be usable to run an external app, and I’m not even sure it would work on Android.
Your post got me thinking, as my KurtMaster2D game is all of my MS-DOS / Windows / PalmOS games, native C code mostly, running as a library, pumped and served via a Unity front end, and occasionally I step on the wrong chunk of memory and kaboom.
If you want it to be a separate process you would also need to choose how will you communicate with child (pipe, SHM, …).I don’t think fork will help you there. I do think Process is a viable option, but you would need to make some wrapper around the library and handle the application loop somehow.
But as first I would examine the crash. Is this library some old library which isn’t maintained anymore? Because if this is the case, only then would I choose running it in sub-process. Because if you go with sub-process it’s another thing you need to maintain, fix bugs, … .