Pull in ObjectHandle changes from framework

This commit is contained in:
Susko3 2024-05-21 13:32:57 +02:00
parent 9ef6d54186
commit 3f2c1b234e
1 changed files with 8 additions and 7 deletions

View File

@ -28,7 +28,7 @@ namespace SDL.Tests
private GCHandle handle; private GCHandle handle;
private readonly bool fromPointer; private readonly bool canFree;
/// <summary> /// <summary>
/// Wraps the provided object with a <see cref="GCHandle" />, using the given <see cref="GCHandleType" />. /// Wraps the provided object with a <see cref="GCHandle" />, using the given <see cref="GCHandleType" />.
@ -38,18 +38,19 @@ namespace SDL.Tests
public ObjectHandle(T target, GCHandleType handleType) public ObjectHandle(T target, GCHandleType handleType)
{ {
handle = GCHandle.Alloc(target, handleType); handle = GCHandle.Alloc(target, handleType);
fromPointer = false; canFree = true;
} }
/// <summary> /// <summary>
/// Recreates an <see cref="ObjectHandle{T}" /> based on the passed <see cref="IntPtr" />. /// Recreates an <see cref="ObjectHandle{T}" /> based on the passed <see cref="IntPtr" />.
/// Disposing this object will not free the handle, the original object must be disposed instead. /// If <paramref name="ownsHandle"/> is <c>true</c>, disposing this object will free the handle.
/// </summary> /// </summary>
/// <param name="handle">Handle.</param> /// <param name="handle"><see cref="Handle"/> from a previously constructed <see cref="ObjectHandle{T}(T, GCHandleType)"/>.</param>
public ObjectHandle(IntPtr handle) /// <param name="ownsHandle">Whether this instance owns the underlying <see cref="GCHandle"/>.</param>
public ObjectHandle(IntPtr handle, bool ownsHandle = false)
{ {
this.handle = GCHandle.FromIntPtr(handle); this.handle = GCHandle.FromIntPtr(handle);
fromPointer = true; canFree = ownsHandle;
} }
/// <summary> /// <summary>
@ -86,7 +87,7 @@ namespace SDL.Tests
public void Dispose() public void Dispose()
{ {
if (!fromPointer && handle.IsAllocated) if (canFree && handle.IsAllocated)
handle.Free(); handle.Free();
} }