dangerous release (possibly memory leak and deadlock)

This commit is contained in:
dennisarfan 2025-07-31 07:40:28 +07:00
parent a1cb6592eb
commit 741d34a5e0
10 changed files with 164 additions and 115 deletions

View file

@ -9,18 +9,19 @@ namespace StitchATon2.Infra.Buffers;
/// <typeparam name="T"></typeparam>
internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
{
private readonly T* _pointer;
private readonly int _count;
internal readonly T* Pointer;
private bool _disposed;
public int Length { get; }
public ref T this[int index] => ref Unsafe.AsRef<T>(_pointer + index);
public ref T this[int index] => ref Unsafe.AsRef<T>(Pointer + index);
public Span<T> Span => new(_pointer, _count);
public Span<T> Span => new(Pointer, Length);
public UnmanagedMemory(int count)
public UnmanagedMemory(int length)
{
_pointer = (T*)NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
_count = count;
Pointer = (T*)NativeMemory.Alloc((nuint)length, (nuint)Unsafe.SizeOf<T>());
Length = length;
}
~UnmanagedMemory() => Dispose();
@ -29,7 +30,7 @@ internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
{
if (!_disposed)
{
NativeMemory.Free(_pointer);
NativeMemory.Free(Pointer);
GC.SuppressFinalize(this);
_disposed = true;
}