Skip to content

Commit

Permalink
Merge pull request #536 from CesiumGS/domain-reload-stability
Browse files Browse the repository at this point in the history
Improve stability on AppDomain reloads.
  • Loading branch information
kring authored Jan 1, 2025
2 parents 0c2625f + 3896a5b commit 0d8f0e0
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 106 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

##### Fixes :wrench:

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

## v1.14.0 - 2024-12-02
Expand Down
38 changes: 29 additions & 9 deletions Reinterop~/CSharpObjectHandleUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ public static IntPtr CopyHandle(IntPtr handle)
return handle;
// Allocate a new GCHandle pointing to the same object.
GCHandle gcHandle = GCHandle.FromIntPtr(handle);
return GCHandle.ToIntPtr(GCHandle.Alloc(gcHandle.Target));
try
{
GCHandle gcHandle = GCHandle.FromIntPtr(handle);
return GCHandle.ToIntPtr(GCHandle.Alloc(gcHandle.Target));
}
catch (Exception)
{
return IntPtr.Zero;
}
}
public static void FreeHandle(IntPtr handle)
Expand All @@ -50,14 +57,13 @@ public static void FreeHandle(IntPtr handle)
{
GCHandle.FromIntPtr(handle).Free();
}
catch (ArgumentException e)
catch (Exception)
{
// The "GCHandle value belongs to a different domain" exception tends
// to happen on AppDomain reload, which is common in Unity.
// Catch the exception to prevent it propagating through our native
// code and blowing things up.
// See: https://github.com/CesiumGS/cesium-unity/issues/18
System.Console.WriteLine(e.ToString());
}
}
Expand All @@ -66,18 +72,32 @@ public static object GetObjectFromHandle(IntPtr handle)
if (handle == IntPtr.Zero)
return null;
return GCHandle.FromIntPtr(handle).Target;
try
{
return GCHandle.FromIntPtr(handle).Target;
}
catch (Exception)
{
return null;
}
}
public static object GetObjectAndFreeHandle(IntPtr handle)
{
if (handle == IntPtr.Zero)
return null;
GCHandle gcHandle = GCHandle.FromIntPtr(handle);
object result = gcHandle.Target;
gcHandle.Free();
return result;
try
{
GCHandle gcHandle = GCHandle.FromIntPtr(handle);
object result = gcHandle.Target;
gcHandle.Free();
return result;
}
catch (Exception)
{
return null;
}
}
public static void ResetHandleObject(IntPtr handle, object newValue)
Expand Down
Loading

0 comments on commit 0d8f0e0

Please sign in to comment.