Skip to content

Commit 0d8f0e0

Browse files
authored
Merge pull request #536 from CesiumGS/domain-reload-stability
Improve stability on AppDomain reloads.
2 parents 0c2625f + 3896a5b commit 0d8f0e0

File tree

5 files changed

+142
-106
lines changed

5 files changed

+142
-106
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
##### Fixes :wrench:
66

7+
- Fixed a bug that could cause a crash on AppDomain reloads.
78
- Fixed a bug that could cause a crash or incorrect textures when multiple `Cesium3DTileset` tiles referenced the same image by URL.
89

910
## v1.14.0 - 2024-12-02

Reinterop~/CSharpObjectHandleUtility.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)