修复 Bug

This commit is contained in:
2026-01-17 07:55:21 +08:00
parent 2a331d769f
commit a27045e0a0
2 changed files with 18 additions and 13 deletions

View File

@@ -9,7 +9,7 @@ namespace SHH.CameraSdk;
/// </summary>
public class SmartFrame : IDisposable
{
public List<string> SubscriberIds { get; } = new List<string>(16);
public ConcurrentQueue<string> SubscriberIds { get; } = new ConcurrentQueue<string>();
#region --- (Private Resources & States) ---
@@ -114,6 +114,8 @@ public class SmartFrame : IDisposable
ScaleType = scaleType;
}
private readonly object _subscriberLock = new(); // 建议在类头部定义此锁
/// <summary>
/// 内部清理:释放衍生数据
/// </summary>
@@ -126,9 +128,14 @@ public class SmartFrame : IDisposable
}
ScaleType = FrameScaleType.None;
// 2. [核心逻辑] 清空订阅者列表
// 注意Clear() 只是把 Count 设为 0底层数组容量不变不会触发 GC
SubscriberIds.Clear();
// 2. [核心逻辑] 线程安全地清空订阅者列表
// 使用 lock 确保在 Clear 的瞬间,没有其他分发线程正在执行 Add 操作
lock (_subscriberLock)
{
// 注意Clear() 只是把 Count 设为 0底层数组容量 (Capacity) 不变
// 这样下次复用时SubscriberIds 不需要重新分配内存,完美符合零拷贝初衷
SubscriberIds.Clear();
}
}
#endregion