SDK 的 Bug 修复
This commit is contained in:
@@ -78,6 +78,8 @@ public class SmartFrame : IDisposable
|
||||
ResetDerivatives();
|
||||
}
|
||||
|
||||
private int _isReturned = 0; // 0: 激活中, 1: 已归还池
|
||||
|
||||
/// <summary>
|
||||
/// [生产者调用] 从帧池取出时激活帧
|
||||
/// 功能:初始化引用计数,标记激活时间戳
|
||||
@@ -86,6 +88,7 @@ public class SmartFrame : IDisposable
|
||||
{
|
||||
// 激活后引用计数设为 1,代表生产者(驱动/管道)持有该帧
|
||||
_refCount = 1;
|
||||
_isReturned = 0; // 激活时重置归还标记
|
||||
// 记录帧被取出池的时间,用于后续延迟计算
|
||||
Timestamp = DateTime.Now;
|
||||
}
|
||||
@@ -155,11 +158,15 @@ public class SmartFrame : IDisposable
|
||||
// 原子递减:线程安全,确保计数准确
|
||||
if (Interlocked.Decrement(ref _refCount) <= 0)
|
||||
{
|
||||
// 1. 彻底清理衍生数据(TargetMat 通常是 new 出来的,必须 Dispose)
|
||||
ResetDerivatives();
|
||||
// 2. 关键:原子抢占归还权。只有成功将 _isReturned 从 0 变为 1 的线程才能执行归还逻辑。
|
||||
if (Interlocked.CompareExchange(ref _isReturned, 1, 0) == 0)
|
||||
{
|
||||
// 3. 彻底清理衍生数据(TargetMat 必须释放)
|
||||
ResetDerivatives();
|
||||
|
||||
// 2. 归还到池中复用 (InternalMat 不释放,继续保留在内存池中)
|
||||
_pool.Return(this);
|
||||
// 4. 安全归还到池中
|
||||
_pool.Return(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user