From a08ef4be2e1561023d99615a7a6f9c99051eef79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Tue, 21 Apr 2026 15:45:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98=E6=B1=A0?= =?UTF-8?q?=E9=83=A8=E5=88=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/ABase/MemoryPool/MemoryPool.Core.cs | 25 ++++++--- .../ABase/MemoryPool/MemoryPoolExtension.cs | 54 ------------------- .../MemoryPool/MemoryPoolExtension.cs.meta | 3 -- Runtime/Audio/AudioData.cs | 40 ++++++-------- Runtime/UI/UIBase/UIBase.cs | 1 + 5 files changed, 36 insertions(+), 87 deletions(-) delete mode 100644 Runtime/ABase/MemoryPool/MemoryPoolExtension.cs delete mode 100644 Runtime/ABase/MemoryPool/MemoryPoolExtension.cs.meta diff --git a/Runtime/ABase/MemoryPool/MemoryPool.Core.cs b/Runtime/ABase/MemoryPool/MemoryPool.Core.cs index 454328c..1f37ee1 100644 --- a/Runtime/ABase/MemoryPool/MemoryPool.Core.cs +++ b/Runtime/ABase/MemoryPool/MemoryPool.Core.cs @@ -67,11 +67,26 @@ namespace AlicizaX 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)] public static void Release(T item) { if (item == null) return; + if (MemoryPool.EnableStrictCheck && ContainsInPool(item)) + throw new InvalidOperationException($"MemoryPool<{typeof(T).Name}>: Double release detected."); + s_ReleaseCount++; if (s_CurrentInUse > 0) @@ -82,9 +97,6 @@ namespace AlicizaX if (s_Count >= s_MaxCapacity) 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) Grow(); @@ -196,9 +208,8 @@ namespace AlicizaX { Array.Clear(s_Stack, 0, s_Count); s_Count = 0; - s_HighWaterMark = 0; - s_PeakInUse = 0; - s_CurrentInUse = 0; + s_HighWaterMark = s_CurrentInUse; + s_PeakInUse = s_CurrentInUse; s_IdleFrames = 0; s_RecentAcquireCount = 0; s_Stack = Array.Empty(); @@ -214,7 +225,7 @@ namespace AlicizaX { return new MemoryPoolInfo( typeof(T), s_Count, - s_AcquireCount - s_ReleaseCount, + s_CurrentInUse, s_AcquireCount, s_ReleaseCount, s_CreateCount, s_HighWaterMark, s_MaxCapacity, diff --git a/Runtime/ABase/MemoryPool/MemoryPoolExtension.cs b/Runtime/ABase/MemoryPool/MemoryPoolExtension.cs deleted file mode 100644 index 2105e79..0000000 --- a/Runtime/ABase/MemoryPool/MemoryPoolExtension.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; - -namespace AlicizaX -{ - /// - /// 内存池对象基类。 - /// - public abstract class MemoryObject : IMemory - { - /// - /// 清理内存对象回收入池。 - /// - public virtual void Clear() - { - } - - /// - /// 从内存池中初始化。 - /// - public abstract void InitFromPool(); - - /// - /// 回收到内存池。 - /// - public abstract void RecycleToPool(); - } - - public static partial class MemoryPool - { - /// - /// 从内存池获取内存对象。 - /// - public static T Alloc() where T : MemoryObject, new() - { - T memory = MemoryPool.Acquire(); - memory.InitFromPool(); - return memory; - } - - /// - /// 将内存对象归还内存池。 - /// - public static void Dealloc(MemoryObject memory) - { - if (memory == null) - { - throw new ArgumentNullException(nameof(memory)); - } - - memory.RecycleToPool(); - MemoryPoolRegistry.Release(memory); - } - } -} diff --git a/Runtime/ABase/MemoryPool/MemoryPoolExtension.cs.meta b/Runtime/ABase/MemoryPool/MemoryPoolExtension.cs.meta deleted file mode 100644 index 2d4491f..0000000 --- a/Runtime/ABase/MemoryPool/MemoryPoolExtension.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: ad13ef73c22340058c4420733a22b580 -timeCreated: 1701273442 \ No newline at end of file diff --git a/Runtime/Audio/AudioData.cs b/Runtime/Audio/AudioData.cs index ce5732d..2691ffd 100644 --- a/Runtime/Audio/AudioData.cs +++ b/Runtime/Audio/AudioData.cs @@ -6,7 +6,7 @@ namespace AlicizaX.Audio.Runtime /// /// 音频数据。 /// - public class AudioData : MemoryObject + public class AudioData : IMemory { /// /// 资源句柄。 @@ -18,23 +18,7 @@ namespace AlicizaX.Audio.Runtime /// public bool InPool { private set; get; } = false; - public override void InitFromPool() - { - } - /// - /// 回收到对象池。 - /// - public override void RecycleToPool() - { - if (!InPool) - { - AssetHandle.Dispose(); - } - - InPool = false; - AssetHandle = null; - } /// /// 生成音频数据。 @@ -47,7 +31,6 @@ namespace AlicizaX.Audio.Runtime AudioData ret = MemoryPool.Acquire(); ret.AssetHandle = assetHandle; ret.InPool = inPool; - ret.InitFromPool(); return ret; } @@ -57,11 +40,22 @@ namespace AlicizaX.Audio.Runtime /// internal static void DeAlloc(AudioData audioData) { - if (audioData != null) - { - MemoryPool.Release(audioData); - audioData.RecycleToPool(); - } + if (audioData == null) + return; + + MemoryPool.Release(audioData); + } + + public void Clear() + { + bool inPool = InPool; + AssetHandle handle = AssetHandle; + + InPool = false; + AssetHandle = null; + + if (!inPool && handle is { IsValid: true }) + handle.Dispose(); } } } diff --git a/Runtime/UI/UIBase/UIBase.cs b/Runtime/UI/UIBase/UIBase.cs index 8715dc3..28ee541 100644 --- a/Runtime/UI/UIBase/UIBase.cs +++ b/Runtime/UI/UIBase/UIBase.cs @@ -193,6 +193,7 @@ namespace AlicizaX.UI.Runtime if (!_eventListenerProxy.IsNull()) { MemoryPool.Release(_eventListenerProxy); + _eventListenerProxy = null; } }