diff --git a/service/tool.go b/service/tool.go index 3f2dd2d..b45ee95 100644 --- a/service/tool.go +++ b/service/tool.go @@ -97,6 +97,8 @@ func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) { defer func() { if err := recover(); err != nil { log.Printf("设备:%d 错误: %v\n", deviceId, err) + // 异常时释放 buf + buf = nil } }() //获取读写锁 @@ -120,21 +122,32 @@ func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) { return nil, frameCount } //获取当前帧 + var tempBuf []byte switch deviceId { case 1: if Device1CurrentFrame.Empty() { return nil, -1 } - buf = Device1CurrentFrame.ToBytes() + tempBuf = Device1CurrentFrame.ToBytes() case 50: if Device50CurrentFrame.Empty() { return nil, -1 } - buf = Device50CurrentFrame.ToBytes() + tempBuf = Device50CurrentFrame.ToBytes() case 73: if Device73CurrentFrame.Empty() { return nil, -1 } + tempBuf = Device73CurrentFrame.ToBytes() + default: + return nil, -1 + } + // 避免直接返回大数组,可考虑复制必要的数据 + if len(tempBuf) > 0 { + buf = make([]byte, len(tempBuf)) + copy(buf, tempBuf) + // 释放临时变量 + tempBuf = nil } return buf, frameCount }