修复内存池部分bug

This commit is contained in:
陈思海 2026-04-21 15:45:27 +08:00
parent 24bb3f9e94
commit a08ef4be2e
5 changed files with 36 additions and 87 deletions

View File

@ -67,11 +67,26 @@ namespace AlicizaX
return new T(); return new T();
} }
[MethodImpl(MethodImplOptions.NoInlining)]
private static bool ContainsInPool(T item)
{
for (int i = 0; i < s_Count; i++)
{
if (ReferenceEquals(s_Stack[i], item))
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release(T item) public static void Release(T item)
{ {
if (item == null) return; if (item == null) return;
if (MemoryPool.EnableStrictCheck && ContainsInPool(item))
throw new InvalidOperationException($"MemoryPool<{typeof(T).Name}>: Double release detected.");
s_ReleaseCount++; s_ReleaseCount++;
if (s_CurrentInUse > 0) if (s_CurrentInUse > 0)
@ -82,9 +97,6 @@ namespace AlicizaX
if (s_Count >= s_MaxCapacity) if (s_Count >= s_MaxCapacity)
return; return;
if (MemoryPool.EnableStrictCheck && s_Count > 0 && ReferenceEquals(s_Stack[s_Count - 1], item))
throw new InvalidOperationException($"MemoryPool<{typeof(T).Name}>: Double release detected.");
if (s_Count == s_Stack.Length) if (s_Count == s_Stack.Length)
Grow(); Grow();
@ -196,9 +208,8 @@ namespace AlicizaX
{ {
Array.Clear(s_Stack, 0, s_Count); Array.Clear(s_Stack, 0, s_Count);
s_Count = 0; s_Count = 0;
s_HighWaterMark = 0; s_HighWaterMark = s_CurrentInUse;
s_PeakInUse = 0; s_PeakInUse = s_CurrentInUse;
s_CurrentInUse = 0;
s_IdleFrames = 0; s_IdleFrames = 0;
s_RecentAcquireCount = 0; s_RecentAcquireCount = 0;
s_Stack = Array.Empty<T>(); s_Stack = Array.Empty<T>();
@ -214,7 +225,7 @@ namespace AlicizaX
{ {
return new MemoryPoolInfo( return new MemoryPoolInfo(
typeof(T), s_Count, typeof(T), s_Count,
s_AcquireCount - s_ReleaseCount, s_CurrentInUse,
s_AcquireCount, s_ReleaseCount, s_AcquireCount, s_ReleaseCount,
s_CreateCount, s_CreateCount,
s_HighWaterMark, s_MaxCapacity, s_HighWaterMark, s_MaxCapacity,

View File

@ -1,54 +0,0 @@
using System;
namespace AlicizaX
{
/// <summary>
/// 内存池对象基类。
/// </summary>
public abstract class MemoryObject : IMemory
{
/// <summary>
/// 清理内存对象回收入池。
/// </summary>
public virtual void Clear()
{
}
/// <summary>
/// 从内存池中初始化。
/// </summary>
public abstract void InitFromPool();
/// <summary>
/// 回收到内存池。
/// </summary>
public abstract void RecycleToPool();
}
public static partial class MemoryPool
{
/// <summary>
/// 从内存池获取内存对象。
/// </summary>
public static T Alloc<T>() where T : MemoryObject, new()
{
T memory = MemoryPool<T>.Acquire();
memory.InitFromPool();
return memory;
}
/// <summary>
/// 将内存对象归还内存池。
/// </summary>
public static void Dealloc(MemoryObject memory)
{
if (memory == null)
{
throw new ArgumentNullException(nameof(memory));
}
memory.RecycleToPool();
MemoryPoolRegistry.Release(memory);
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: ad13ef73c22340058c4420733a22b580
timeCreated: 1701273442

View File

@ -6,7 +6,7 @@ namespace AlicizaX.Audio.Runtime
/// <summary> /// <summary>
/// 音频数据。 /// 音频数据。
/// </summary> /// </summary>
public class AudioData : MemoryObject public class AudioData : IMemory
{ {
/// <summary> /// <summary>
/// 资源句柄。 /// 资源句柄。
@ -18,23 +18,7 @@ namespace AlicizaX.Audio.Runtime
/// </summary> /// </summary>
public bool InPool { private set; get; } = false; public bool InPool { private set; get; } = false;
public override void InitFromPool()
{
}
/// <summary>
/// 回收到对象池。
/// </summary>
public override void RecycleToPool()
{
if (!InPool)
{
AssetHandle.Dispose();
}
InPool = false;
AssetHandle = null;
}
/// <summary> /// <summary>
/// 生成音频数据。 /// 生成音频数据。
@ -47,7 +31,6 @@ namespace AlicizaX.Audio.Runtime
AudioData ret = MemoryPool.Acquire<AudioData>(); AudioData ret = MemoryPool.Acquire<AudioData>();
ret.AssetHandle = assetHandle; ret.AssetHandle = assetHandle;
ret.InPool = inPool; ret.InPool = inPool;
ret.InitFromPool();
return ret; return ret;
} }
@ -57,11 +40,22 @@ namespace AlicizaX.Audio.Runtime
/// <param name="audioData"></param> /// <param name="audioData"></param>
internal static void DeAlloc(AudioData audioData) internal static void DeAlloc(AudioData audioData)
{ {
if (audioData != null) if (audioData == null)
{ return;
MemoryPool.Release(audioData); MemoryPool.Release(audioData);
audioData.RecycleToPool();
} }
public void Clear()
{
bool inPool = InPool;
AssetHandle handle = AssetHandle;
InPool = false;
AssetHandle = null;
if (!inPool && handle is { IsValid: true })
handle.Dispose();
} }
} }
} }

View File

@ -193,6 +193,7 @@ namespace AlicizaX.UI.Runtime
if (!_eventListenerProxy.IsNull()) if (!_eventListenerProxy.IsNull())
{ {
MemoryPool.Release(_eventListenerProxy); MemoryPool.Release(_eventListenerProxy);
_eventListenerProxy = null;
} }
} }