-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skin Fixer: Fix potential ref leak + add SRH
`SafeResourceHandle` wraps a `ResourceHandle*` with auto `IncRef` / `DecRef`, to further help prevent leaks.
- Loading branch information
Showing
2 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; | ||
|
||
namespace Penumbra.Interop.SafeHandles; | ||
|
||
public unsafe class SafeResourceHandle : SafeHandle | ||
{ | ||
public ResourceHandle* ResourceHandle => (ResourceHandle*)handle; | ||
|
||
public override bool IsInvalid => handle == 0; | ||
|
||
public SafeResourceHandle(ResourceHandle* handle, bool incRef, bool ownsHandle = true) : base(0, ownsHandle) | ||
{ | ||
if (incRef && !ownsHandle) | ||
throw new ArgumentException("Non-owning SafeResourceHandle with IncRef is unsupported"); | ||
if (incRef && handle != null) | ||
handle->IncRef(); | ||
SetHandle((nint)handle); | ||
} | ||
|
||
public static SafeResourceHandle CreateInvalid() | ||
=> new(null, incRef: false); | ||
|
||
protected override bool ReleaseHandle() | ||
{ | ||
var handle = Interlocked.Exchange(ref this.handle, 0); | ||
if (handle != 0) | ||
((ResourceHandle*)handle)->DecRef(); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters