修复 Bug
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user