@@ -37,8 +37,15 @@ public static IntPtr CopyHandle(IntPtr handle)
3737 return handle;
3838
3939 // Allocate a new GCHandle pointing to the same object.
40- GCHandle gcHandle = GCHandle.FromIntPtr(handle);
41- return GCHandle.ToIntPtr(GCHandle.Alloc(gcHandle.Target));
40+ try
41+ {
42+ GCHandle gcHandle = GCHandle.FromIntPtr(handle);
43+ return GCHandle.ToIntPtr(GCHandle.Alloc(gcHandle.Target));
44+ }
45+ catch (Exception)
46+ {
47+ return IntPtr.Zero;
48+ }
4249 }
4350
4451 public static void FreeHandle(IntPtr handle)
@@ -50,14 +57,13 @@ public static void FreeHandle(IntPtr handle)
5057 {
5158 GCHandle.FromIntPtr(handle).Free();
5259 }
53- catch (ArgumentException e )
60+ catch (Exception )
5461 {
5562 // The "GCHandle value belongs to a different domain" exception tends
5663 // to happen on AppDomain reload, which is common in Unity.
5764 // Catch the exception to prevent it propagating through our native
5865 // code and blowing things up.
5966 // See: https://github.com/CesiumGS/cesium-unity/issues/18
60- System.Console.WriteLine(e.ToString());
6167 }
6268 }
6369
@@ -66,18 +72,32 @@ public static object GetObjectFromHandle(IntPtr handle)
6672 if (handle == IntPtr.Zero)
6773 return null;
6874
69- return GCHandle.FromIntPtr(handle).Target;
75+ try
76+ {
77+ return GCHandle.FromIntPtr(handle).Target;
78+ }
79+ catch (Exception)
80+ {
81+ return null;
82+ }
7083 }
7184
7285 public static object GetObjectAndFreeHandle(IntPtr handle)
7386 {
7487 if (handle == IntPtr.Zero)
7588 return null;
7689
77- GCHandle gcHandle = GCHandle.FromIntPtr(handle);
78- object result = gcHandle.Target;
79- gcHandle.Free();
80- return result;
90+ try
91+ {
92+ GCHandle gcHandle = GCHandle.FromIntPtr(handle);
93+ object result = gcHandle.Target;
94+ gcHandle.Free();
95+ return result;
96+ }
97+ catch (Exception)
98+ {
99+ return null;
100+ }
81101 }
82102
83103 public static void ResetHandleObject(IntPtr handle, object newValue)
0 commit comments