From 2a120d85f41fe4a2895f2cb0f2af9e75051b5b0d Mon Sep 17 00:00:00 2001 From: lijun Date: Thu, 23 Jan 2025 22:40:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E5=B8=A7by?= =?UTF-8?q?tes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/tool.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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 }