内存泄漏解决测试

This commit is contained in:
lijun 2025-01-22 14:46:23 +08:00
parent 02a7738c49
commit fdc3407b6d
2 changed files with 12 additions and 13 deletions

View File

@ -202,10 +202,10 @@ func GetRealTimeImage(c *gin.Context) {
subscribeAndHandleMessagesV3(ws, deviceIdInt)
}
func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) {
// 生成唯一连接 uuid
con_id := uuid.New().String()
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
online_conn_key := "device_" + strconv.Itoa(deviceId) + "_online_conn_ids"
// 加入设备在线连接集合
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
//图片计数器
@ -217,16 +217,16 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
//计算帧率
for {
//从service获取当前帧
c := service.GetDeviceCurrentFrameV2(&img, device_id)
c := service.GetDeviceCurrentFrameV2(&img, deviceId)
if c != count {
if c == -1 {
log.Printf("device:%d get frame err!", device_id)
log.Printf("device:%d get frame err!", deviceId)
worker.SetRedisSetRemove(online_conn_key, con_id)
break
}
//将img转[]byte
if img.Empty() {
log.Printf("device:%d img is empty! count = %d \n", device_id, c)
log.Printf("device:%d img is empty! count = %d \n", deviceId, c)
} else {
//gocv.Matrix转为jpeg
buf, err := gocv.IMEncode(".jpg", img)
@ -248,9 +248,10 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
if err5 != nil {
log.Printf("img close err:%v", err)
}
buf.Close()
t++
if t%50 == 0 {
log.Printf("device:%d send frame count:%d,img is closed", device_id, c)
log.Printf("device:%d send frame count:%d,img is closed", deviceId, c)
}
}
} else {
@ -270,14 +271,9 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
t_count++
}
err5 := img.Close()
if err5 != nil {
log.Printf("img close err:%v", err5)
}
// 查看是否还有其他连接,没有则设置 is_play 为 0
if worker.IsContainKey(online_conn_key) == false {
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)
log.Printf("device_id: %d has set is_play to 0", device_id)
worker.SetRedisWithExpire(strconv.Itoa(deviceId)+"_is_play", "1", time.Minute*5)
log.Printf("device_id: %d has set is_play to 0", deviceId)
}
}

View File

@ -85,6 +85,9 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error {
if !ok {
return fmt.Errorf("设备:%s 当前帧计数不存在", deviceId)
}
if frameCount%10 == 0 {
log.Printf("设备:%d 当前帧: %d 已更新\n", deviceId, frameCount)
}
frameCount++
DeviceFrameCount[deviceId] = frameCount
return nil