Fix passing weak managed string handles in marshallers

This commit is contained in:
2023-01-22 17:28:44 +02:00
parent be720257ca
commit a917567e92
2 changed files with 20 additions and 9 deletions

View File

@@ -331,6 +331,17 @@ namespace FlaxEngine
[System.Diagnostics.DebuggerStepThrough]
internal static unsafe IntPtr ToNative(string str)
{
if (str == null)
return IntPtr.Zero;
else if (str == string.Empty)
return ManagedHandle.ToIntPtr(EmptyStringHandle);
Assert.IsTrue(str.Length > 0);
return ManagedHandle.ToIntPtr(ManagedHandle.Alloc(str));
}
[System.Diagnostics.DebuggerStepThrough]
internal static unsafe IntPtr ToNativeWeak(string str)
{
if (str == null)
return IntPtr.Zero;
@@ -969,7 +980,7 @@ namespace FlaxEngine
{
if (managed == null)
return IntPtr.Zero;
return ManagedHandle.ToIntPtr(ManagedHandle.Alloc(managed, GCHandleType.Weak));
return ManagedHandle.ToIntPtr(ManagedHandle.Alloc(managed));
}
public static void Free(IntPtr unmanaged) => ManagedString.Free(unmanaged);
@@ -1718,7 +1729,7 @@ namespace FlaxEngine
IntPtr managedPtr;
if (type == typeof(string))
managedPtr = ManagedString.ToNative(managedValue as string);
managedPtr = ManagedString.ToNativeWeak(managedValue as string);
else if (type.IsPointer)
{
if (Pointer.Unbox(managedValue) == null)
@@ -2242,19 +2253,19 @@ namespace FlaxEngine
[UnmanagedCallersOnly]
internal static IntPtr NewStringUTF16(char* text, int length)
{
return ManagedString.ToNative(new string(new ReadOnlySpan<char>(text, length)));
return ManagedString.ToNativeWeak(new string(new ReadOnlySpan<char>(text, length)));
}
[UnmanagedCallersOnly]
internal static IntPtr NewString(sbyte* text)
{
return ManagedString.ToNative(new string(text));
return ManagedString.ToNativeWeak(new string(text));
}
[UnmanagedCallersOnly]
internal static IntPtr NewStringLength(sbyte* text, int length)
{
return ManagedString.ToNative(new string(text, 0, length));
return ManagedString.ToNativeWeak(new string(text, 0, length));
}
/// <summary>

View File

@@ -78,7 +78,7 @@ namespace FlaxEngine
if (returnValue == null)
return IntPtr.Zero;
if (typeof(TRet) == typeof(string))
return ManagedString.ToNative(Unsafe.As<string>(returnValue));
return ManagedString.ToNativeWeak(Unsafe.As<string>(returnValue));
if (typeof(TRet) == typeof(IntPtr))
return (IntPtr)(object)returnValue;
if (typeof(TRet) == typeof(ManagedHandle))
@@ -102,7 +102,7 @@ namespace FlaxEngine
if (returnObject == null)
return IntPtr.Zero;
if (returnType == typeof(string))
return ManagedString.ToNative(Unsafe.As<string>(returnObject));
return ManagedString.ToNativeWeak(Unsafe.As<string>(returnObject));
if (returnType == typeof(IntPtr))
return (IntPtr)returnObject;
if (returnType == typeof(ManagedHandle))
@@ -123,7 +123,7 @@ namespace FlaxEngine
if (returnValue == null)
return IntPtr.Zero;
if (typeof(TRet) == typeof(string))
return ManagedString.ToNative(Unsafe.As<string>(returnValue));
return ManagedString.ToNativeWeak(Unsafe.As<string>(returnValue));
if (typeof(TRet) == typeof(IntPtr))
return (IntPtr)(object)returnValue;
if (typeof(TRet) == typeof(ManagedHandle))
@@ -160,7 +160,7 @@ namespace FlaxEngine
if (returnObject == null)
return IntPtr.Zero;
if (returnType == typeof(string))
return ManagedString.ToNative(Unsafe.As<string>(returnObject));
return ManagedString.ToNativeWeak(Unsafe.As<string>(returnObject));
if (returnType == typeof(IntPtr))
return (IntPtr)(object)returnObject;
if (returnType == typeof(ManagedHandle))