Improve stability by fixing various crashes with dotnet7
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,17 +561,6 @@ namespace FlaxEngine.Interop
|
||||
return ManagedString.ToNativeWeak(new string(text, 0, length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a managed copy of the value, and stores it in a boxed reference.
|
||||
/// </summary>
|
||||
[UnmanagedCallersOnly]
|
||||
internal static ManagedHandle BoxValue(ManagedHandle typeHandle, IntPtr valuePtr)
|
||||
{
|
||||
Type type = Unsafe.As<Type>(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a managed copy of the value, and stores it in a boxed reference.
|
||||
/// </summary>
|
||||
[UnmanagedCallersOnly]
|
||||
internal static ManagedHandle BoxValue(ManagedHandle typeHandle, IntPtr valuePtr)
|
||||
{
|
||||
Type type = Unsafe.As<Type>(typeHandle.Target);
|
||||
object value = MarshalToManaged(valuePtr, type);
|
||||
return ManagedHandle.Alloc(value, GCHandleType.Weak);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the address of the boxed value type.
|
||||
/// </summary>
|
||||
@@ -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]
|
||||
|
||||
@@ -93,8 +93,8 @@ public:
|
||||
/// </summary>
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<void*, void*>(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<void*, void*, bool>(NewGCHandlePtr, obj, pinned);
|
||||
}
|
||||
|
||||
MGCHandle MCore::GCHandle::NewWeak(MObject* obj, bool trackResurrection)
|
||||
{
|
||||
ASSERT(obj);
|
||||
static void* NewGCHandleWeakPtr = GetStaticMethodPointer(TEXT("NewGCHandleWeak"));
|
||||
return (MGCHandle)CallStaticMethod<void*, void*, bool>(NewGCHandleWeakPtr, obj, trackResurrection);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user