From 596389540a602329116956dc7a7c8dfa53cda659 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 31 十二月 2025 11:19:49 +0800
Subject: [PATCH] 修复一部分内存泄露bug。
---
LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.cs | 42 +++++++------
LB_VisionProcesses/Cameras/CameraConfig.cs | 36 ++++++++++-
LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.Designer.cs | 1
LB_SmartVision/ProcessRun/ProcessContext.cs | 33 ++++++++++
LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs | 11 +++
5 files changed, 97 insertions(+), 26 deletions(-)
diff --git a/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.Designer.cs b/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.Designer.cs
index afc3cce..217409e 100644
--- a/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.Designer.cs
+++ b/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.Designer.cs
@@ -13,6 +13,7 @@
/// <param name="disposing">濡傛灉搴旈噴鏀炬墭绠¤祫婧愶紝涓� true锛涘惁鍒欎负 false銆�</param>
protected override void Dispose(bool disposing)
{
+ this.isClosed = true;
if (disposing && (components != null))
{
components.Dispose();
diff --git a/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.cs b/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.cs
index b9febc4..1642a11 100644
--- a/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.cs
+++ b/LB_SmartVision/Forms/Pages/ProcessPage/ProcessControl.cs
@@ -43,6 +43,11 @@
if (ProcessRunBll != null)
this.label1.Text = $"鎬绘暟锛歿ProcessRunBll.total}";
+ this.isClosed = false ;
+ var TaskPhotoContinue = Task.Factory.StartNew(() =>
+ {
+ ThreadCircleRun();
+ });
}
public void SetTitle(string title)
@@ -149,37 +154,36 @@
if (isCircleRuning)
{
- threadCircleRun = new Thread(ThreadCircleRun);
- threadCircleRun.IsBackground = true;
- threadCircleRun.Start();
+ this.mAutoResetEvent.Set();
}
else
{
isCircleRuning = false;
- threadCircleRun.Abort();
}
+ if (!isCircleRuning)
+ {
+ ProcessRunBll.LogInfo($"{ProcessRunBll.Name}鍏抽棴杩炵画杩愯", LogInfoType.PASS);
+ return;
+ }
+
+ ProcessRunBll.LogInfo($"{ProcessRunBll.Name}寮�鍚繛缁繍琛�", LogInfoType.WARN);
}
catch { }
}
-
- Thread threadCircleRun = null;
+ private AutoResetEvent mAutoResetEvent = new AutoResetEvent(false);
+ bool isClosed = false;
void ThreadCircleRun()
{
- if (!isCircleRuning)
+ while (!isClosed)
{
- ProcessRunBll.LogInfo($"{ProcessRunBll.Name}鍏抽棴杩炵画杩愯", LogInfoType.PASS);
- return;
- }
-
- ProcessRunBll.LogInfo($"{ProcessRunBll.Name}寮�鍚繛缁繍琛�", LogInfoType.WARN);
-
- while (isCircleRuning)
- {
- //this.BeginInvoke(new Action(() =>
- //{
try
{
+ if (isCircleRuning == false)
+ {
+ this.mAutoResetEvent.WaitOne();
+ }
+
ProcessRunBll.LogInfo($"{ProcessRunBll.Name}寮�濮嬭繍琛�", LogInfoType.WARN);
ClearObj();
bool result = ProcessRunBll.Run();
@@ -192,9 +196,7 @@
ShowHoImage(RecordImage);
}
catch { }
- //}));
-
- Thread.Sleep(100);
+ Thread.Sleep(33);
}
}
diff --git a/LB_SmartVision/ProcessRun/ProcessContext.cs b/LB_SmartVision/ProcessRun/ProcessContext.cs
index 6f32ad5..4081006 100644
--- a/LB_SmartVision/ProcessRun/ProcessContext.cs
+++ b/LB_SmartVision/ProcessRun/ProcessContext.cs
@@ -9,6 +9,7 @@
using LB_VisionProcesses.Processes.ScriptTool;
using OpenCvSharp;
using System.Collections.Concurrent;
+using System.Windows.Media.Media3D;
namespace LB_SmartVision.ProcessRun
{
@@ -489,7 +490,37 @@
if (dicContext.TryGetValue(ProcessName, out IProcess obj)
&& obj is IProcess process)
{
- process.InputImage = null;
+ //process.InputImage = null;
+
+ if (process.InputImage != null)
+ {
+ if (process.InputImage is HObject)
+ ((HObject)process.InputImage).Dispose();
+ else if (process.InputImage is Mat)
+ ((Mat)process.InputImage).Dispose();
+ else if (process.InputImage is Bitmap)
+ ((Bitmap)process.InputImage).Dispose();
+
+ process.InputImage = null;
+ }
+
+ if (process.OutputImage != null)
+ {
+ if (process.OutputImage is HObject)
+ ((HObject)process.OutputImage).Dispose();
+ else if (process.OutputImage is Mat)
+ ((Mat)process.OutputImage).Dispose();
+ else if (process.OutputImage is Bitmap)
+ ((Bitmap)process.OutputImage).Dispose();
+
+ process.OutputImage = null;
+ }
+
+ if (process.Record != null)
+ {
+ process.Record.Dispose();
+ process.Record = null;
+ }
UpdateInputs(process);
// 涓嶅悓鑺傜偣璺宠繃鐨勬柟寮忎笉鍚�
diff --git a/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs b/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs
index 8e76cd9..2c7a2c6 100644
--- a/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs
+++ b/LB_VisionProcesses/Alogrithms/BaseAlgorithm/TAlgorithm.cs
@@ -304,7 +304,16 @@
Result = true;
bCompleted = false;
Msg = "";
- OutputImage = null;
+ if (OutputImage != null)
+ {
+ if (OutputImage is HObject)
+ ((HObject)OutputImage).Dispose();
+ else if (OutputImage is Mat)
+ ((Mat)OutputImage).Dispose();
+ else if (OutputImage is Bitmap)
+ ((Bitmap)OutputImage).Dispose();
+ OutputImage = null;
+ }
if (Record != null)
Record.Dispose();
diff --git a/LB_VisionProcesses/Cameras/CameraConfig.cs b/LB_VisionProcesses/Cameras/CameraConfig.cs
index 8210a2e..4bec6f7 100644
--- a/LB_VisionProcesses/Cameras/CameraConfig.cs
+++ b/LB_VisionProcesses/Cameras/CameraConfig.cs
@@ -1,7 +1,9 @@
-锘縰sing Newtonsoft.Json;
+锘縰sing HalconDotNet;
+using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenCvSharp;
using OpenCvSharp.Extensions;
+using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
@@ -302,21 +304,47 @@
Msg = "";
if (OutputImage != null)
{
- if (OutputImage is Bitmap)
- {
+ if (OutputImage is HObject)
+ ((HObject)OutputImage).Dispose();
+ else if (OutputImage is Mat)
+ ((Mat)OutputImage).Dispose();
+ else if (OutputImage is Bitmap)
((Bitmap)OutputImage).Dispose();
- }
+
OutputImage = null;
}
if (Record != null)
{
Record.Dispose();
+ Record = null;
}
}
public override void Dispose()
{
+ if (InputImage != null)
+ {
+ if (InputImage is HObject)
+ ((HObject)InputImage).Dispose();
+ else if (InputImage is Mat)
+ ((Mat)InputImage).Dispose();
+ else if (InputImage is Bitmap)
+ ((Bitmap)InputImage).Dispose();
+ InputImage = null;
+ }
+
+ if (OutputImage != null)
+ {
+ if (OutputImage is HObject)
+ ((HObject)OutputImage).Dispose();
+ else if (OutputImage is Mat)
+ ((Mat)OutputImage).Dispose();
+ else if (OutputImage is Bitmap)
+ ((Bitmap)OutputImage).Dispose();
+
+ OutputImage = null;
+ }
return;
}
--
Gitblit v1.9.3