Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading