diff --git a/Source/Engine/Animations/Graph/AnimGraph.Custom.cpp b/Source/Engine/Animations/Graph/AnimGraph.Custom.cpp
index b1b49742f..d861fa12a 100644
--- a/Source/Engine/Animations/Graph/AnimGraph.Custom.cpp
+++ b/Source/Engine/Animations/Graph/AnimGraph.Custom.cpp
@@ -205,7 +205,7 @@ bool AnimGraph::InitCustomNode(Node* node)
// Allocate managed node object (create GC handle to prevent destruction)
MObject* obj = type->CreateInstance();
- const MGCHandle handleGC = MCore::GCHandle::New(obj, false);
+ const MGCHandle handleGC = MCore::GCHandle::New(obj);
// Initialize node
InternalInitData initData;
diff --git a/Source/Engine/Core/Types/Variant.cpp b/Source/Engine/Core/Types/Variant.cpp
index 8d2d3a925..33ffde349 100644
--- a/Source/Engine/Core/Types/Variant.cpp
+++ b/Source/Engine/Core/Types/Variant.cpp
@@ -618,9 +618,9 @@ Variant::Variant(MObject* v)
: Type(VariantType::ManagedObject, v ? MCore::Object::GetClass(v) : nullptr)
{
#if USE_NETCORE
- AsUint64 = v ? MCore::GCHandle::New(v, true) : 0;
+ AsUint64 = v ? MCore::GCHandle::New(v) : 0;
#else
- AsUint = v ? MCore::GCHandle::New(v, true) : 0;
+ AsUint = v ? MCore::GCHandle::New(v) : 0;
#endif
}
@@ -1094,9 +1094,9 @@ Variant& Variant::operator=(const Variant& other)
break;
case VariantType::ManagedObject:
#if USE_NETCORE
- AsUint64 = other.AsUint64 ? MCore::GCHandle::New(MCore::GCHandle::GetTarget(other.AsUint64), true) : 0;
+ AsUint64 = other.AsUint64 ? MCore::GCHandle::New(MCore::GCHandle::GetTarget(other.AsUint64)) : 0;
#elif USE_MONO
- AsUint = other.AsUint ? MCore::GCHandle::New(MCore::GCHandle::GetTarget(other.AsUint), true) : 0;
+ AsUint = other.AsUint ? MCore::GCHandle::New(MCore::GCHandle::GetTarget(other.AsUint)) : 0;
#endif
break;
case VariantType::Null:
@@ -2660,9 +2660,9 @@ void Variant::SetManagedObject(MObject* object)
if (Type.Type != VariantType::ManagedObject)
SetType(VariantType(VariantType::ManagedObject, MCore::Object::GetClass(object)));
#if USE_NETCORE
- AsUint64 = MCore::GCHandle::New(object, true);
+ AsUint64 = MCore::GCHandle::New(object);
#else
- AsUint = MCore::GCHandle::New(object, true);
+ AsUint = MCore::GCHandle::New(object);
#endif
}
else
@@ -3708,9 +3708,9 @@ void Variant::AllocStructure()
Type.Type = VariantType::ManagedObject;
#if USE_NETCORE
- AsUint64 = MCore::GCHandle::New(instance, true);
+ AsUint64 = MCore::GCHandle::New(instance);
#else
- AsUint = MCore::GCHandle::New(instance, true);
+ AsUint = MCore::GCHandle::New(instance);
#endif
#endif
}
diff --git a/Source/Engine/Engine/NativeInterop.Managed.cs b/Source/Engine/Engine/NativeInterop.Managed.cs
index 42fccf204..9ff5939d4 100644
--- a/Source/Engine/Engine/NativeInterop.Managed.cs
+++ b/Source/Engine/Engine/NativeInterop.Managed.cs
@@ -420,7 +420,6 @@ namespace FlaxEngine.Interop
else if (type == GCHandleType.Weak || type == GCHandleType.WeakTrackResurrection)
weakPool.Add(handle, value);
}
-
return handle;
}
@@ -439,7 +438,6 @@ namespace FlaxEngine.Interop
else if (weakPoolOther.TryGetValue(handle, out value))
return value;
}
-
throw new Exception("Invalid ManagedHandle");
}
@@ -456,9 +454,8 @@ namespace FlaxEngine.Interop
weakPool[handle] = value;
else if (weakPoolOther.ContainsKey(handle))
weakPoolOther[handle] = value;
- else
- throw new Exception("Invalid ManagedHandle");
}
+ throw new Exception("Invalid ManagedHandle");
}
internal static void FreeHandle(IntPtr handle)
@@ -478,7 +475,6 @@ namespace FlaxEngine.Interop
else if (weakPoolOther.Remove(handle))
return;
}
-
throw new Exception("Invalid ManagedHandle");
}
}
diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs
index 9db162fe5..03443063c 100644
--- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs
+++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs
@@ -561,17 +561,6 @@ namespace FlaxEngine.Interop
return ManagedString.ToNativeWeak(new string(text, 0, length));
}
- ///
- /// Creates a managed copy of the value, and stores it in a boxed reference.
- ///
- [UnmanagedCallersOnly]
- internal static ManagedHandle BoxValue(ManagedHandle typeHandle, IntPtr valuePtr)
- {
- Type type = Unsafe.As(typeHandle.Target);
- object value = MarshalToManaged(valuePtr, type);
- return ManagedHandle.Alloc(value, GCHandleType.Weak);
- }
-
[UnmanagedCallersOnly]
internal static ManagedHandle GetObjectType(ManagedHandle handle)
{
@@ -614,6 +603,17 @@ namespace FlaxEngine.Interop
return result;
}
+ ///
+ /// Creates a managed copy of the value, and stores it in a boxed reference.
+ ///
+ [UnmanagedCallersOnly]
+ internal static ManagedHandle BoxValue(ManagedHandle typeHandle, IntPtr valuePtr)
+ {
+ Type type = Unsafe.As(typeHandle.Target);
+ object value = MarshalToManaged(valuePtr, type);
+ return ManagedHandle.Alloc(value, GCHandleType.Weak);
+ }
+
///
/// Returns the address of the boxed value type.
///
@@ -1019,17 +1019,13 @@ namespace FlaxEngine.Interop
[UnmanagedCallersOnly]
internal static ManagedHandle NewGCHandle(ManagedHandle valueHandle, byte pinned)
{
- object value = valueHandle.Target;
- ManagedHandle handle = ManagedHandle.Alloc(value, pinned != 0 ? GCHandleType.Pinned : GCHandleType.Normal);
- return handle;
+ return ManagedHandle.Alloc(valueHandle.Target, pinned != 0 ? GCHandleType.Pinned : GCHandleType.Normal);
}
[UnmanagedCallersOnly]
internal static ManagedHandle NewGCHandleWeak(ManagedHandle valueHandle, byte trackResurrection)
{
- object value = valueHandle.Target;
- ManagedHandle handle = ManagedHandle.Alloc(value, trackResurrection != 0 ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);
- return handle;
+ return ManagedHandle.Alloc(valueHandle.Target, trackResurrection != 0 ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);
}
[UnmanagedCallersOnly]
diff --git a/Source/Engine/Scripting/ManagedCLR/MCore.h b/Source/Engine/Scripting/ManagedCLR/MCore.h
index ca1193dde..497c27657 100644
--- a/Source/Engine/Scripting/ManagedCLR/MCore.h
+++ b/Source/Engine/Scripting/ManagedCLR/MCore.h
@@ -93,8 +93,8 @@ public:
///
struct FLAXENGINE_API GCHandle
{
- static MGCHandle New(MObject* obj, bool pinned);
- static MGCHandle NewWeak(MObject* obj, bool trackResurrection);
+ static MGCHandle New(MObject* obj, bool pinned = false);
+ static MGCHandle NewWeak(MObject* obj, bool trackResurrection = false);
static MObject* GetTarget(const MGCHandle& handle);
static void Free(const MGCHandle& handle);
};
diff --git a/Source/Engine/Scripting/ManagedCLR/MUtils.cpp b/Source/Engine/Scripting/ManagedCLR/MUtils.cpp
index 653319b6b..d97ff92cd 100644
--- a/Source/Engine/Scripting/ManagedCLR/MUtils.cpp
+++ b/Source/Engine/Scripting/ManagedCLR/MUtils.cpp
@@ -995,7 +995,7 @@ MClass* MUtils::GetClass(const Variant& value)
return stdTypes.MatrixClass;
case VariantType::Array:
case VariantType::Dictionary:
- return GetClass(value.Type);
+ break;
case VariantType::Object:
return value.AsObject ? value.AsObject->GetClass() : nullptr;
case VariantType::Asset:
@@ -1004,10 +1004,14 @@ MClass* MUtils::GetClass(const Variant& value)
case VariantType::Enum:
return Scripting::FindClass(StringAnsiView(value.Type.TypeName));
case VariantType::ManagedObject:
- return MCore::Object::GetClass((MObject*)value);
+ {
+ MObject* obj = (MObject*)value;
+ if (obj)
+ return MCore::Object::GetClass(obj);
+ }
default: ;
}
- return nullptr;
+ return GetClass(value.Type);
}
MTypeObject* MUtils::GetType(MObject* object)
diff --git a/Source/Engine/Scripting/Runtime/DotNet.cpp b/Source/Engine/Scripting/Runtime/DotNet.cpp
index 23b534d22..af324b31c 100644
--- a/Source/Engine/Scripting/Runtime/DotNet.cpp
+++ b/Source/Engine/Scripting/Runtime/DotNet.cpp
@@ -327,6 +327,7 @@ void MCore::Object::Init(MObject* obj)
MClass* MCore::Object::GetClass(MObject* obj)
{
+ ASSERT(obj);
static void* GetObjectTypePtr = GetStaticMethodPointer(TEXT("GetObjectType"));
void* classHandle = CallStaticMethod(GetObjectTypePtr, obj);
return GetOrCreateClass(classHandle);
@@ -397,12 +398,14 @@ void* MCore::Array::GetAddress(const MArray* obj)
MGCHandle MCore::GCHandle::New(MObject* obj, bool pinned)
{
+ ASSERT(obj);
static void* NewGCHandlePtr = GetStaticMethodPointer(TEXT("NewGCHandle"));
return (MGCHandle)CallStaticMethod(NewGCHandlePtr, obj, pinned);
}
MGCHandle MCore::GCHandle::NewWeak(MObject* obj, bool trackResurrection)
{
+ ASSERT(obj);
static void* NewGCHandleWeakPtr = GetStaticMethodPointer(TEXT("NewGCHandleWeak"));
return (MGCHandle)CallStaticMethod(NewGCHandleWeakPtr, obj, trackResurrection);
}
diff --git a/Source/Engine/Scripting/ScriptingObject.cpp b/Source/Engine/Scripting/ScriptingObject.cpp
index 785487f23..e534c7dbb 100644
--- a/Source/Engine/Scripting/ScriptingObject.cpp
+++ b/Source/Engine/Scripting/ScriptingObject.cpp
@@ -223,7 +223,7 @@ void ScriptingObject::SetManagedInstance(MObject* instance)
#if USE_NETCORE
_gcHandle = (MGCHandle)instance;
#elif !COMPILE_WITHOUT_CSHARP
- _gcHandle = MCore::GCHandle::New(instance, false);
+ _gcHandle = MCore::GCHandle::New(instance);
#endif
}
@@ -433,7 +433,7 @@ void ManagedScriptingObject::SetManagedInstance(MObject* instance)
#if USE_NETCORE
_gcHandle = (MGCHandle)instance;
#elif !COMPILE_WITHOUT_CSHARP
- _gcHandle = MCore::GCHandle::NewWeak(instance, false);
+ _gcHandle = MCore::GCHandle::NewWeak(instance);
#endif
}