系统相关
首页 > 系统相关> > 49、Halcon内存泄露

49、Halcon内存泄露

作者:互联网

一、Hobject重复使用

定义出来的图形变量不可以重复利用,即输入和输出不可用同一变量,这样会造成变量不断地叠加,像栈一样,每执行一次内存就增加一个图形变量的大小.到最后释放时只会释放栈中的最后一个.

Dim HImg As HObject = Nothing

HOperatorSet.GenEmptyObj(HImg)

HImg.Dispose()

HOperatorSet.ReadImage(HImg, "c:/222.bmp")

While 1

'输入与输出同一变量,不断叠加使用

HOperatorSet.CropDomain(HImg, HImg)

End While

'最后释放只会释放一次。即队列中的最后一个。

HImg.Dispose()

End Sub

正确处理方法:

没处理一次就释放一次,若想使得处理前与处理后的结果都放在同一变量下,可以增加一个中间变量,这个中间变量也是用一次就释放一次.

Dim HImg As HObject = Nothing

HOperatorSet.GenEmptyObj(HImg)

HImg.Dispose()

HOperatorSet.ReadImage(HImg, "c:/222.bmp")

Dim TempObject As HObject = Nothing

While 1

'拷贝到中间变量上

HOperatorSet.CopyObj(HImg, TempObject, 1, -1)

'原变量马上释放掉

HImg.Dispose()

'用中间变量进行处理,处理结果放在原变量上

HOperatorSet.CropDomain(TempObject, HImg)

'中间变量马上释放掉

TempObject.Dispose()

End While

 

'最后把所有图形变量都释放掉

HImg.Dispose()

TempObject.Dispose()

二、变量定义技巧

像这样调用

Dim CurBmp As Bitmap = Nothing

Dim CurHobject As HObject

CurBmp = FrmImagingCameTool.SnapNetImg(CameraT)

CurHobject = ImageCoverTool.SwitchHalconImage(CurBmp)

也会使内存急剧上涨。所以把变量定义改成一下格式,每次对变量赋值都会释放一次变量数据。

Private mCurBmp As Bitmap

Public Property CurBmp() As Bitmap

Get

Return mCurBmp

End Get

Set(ByVal value As Bitmap)

If mCurBmp IsNot Nothing Then

mCurBmp.Dispose()

End If

mCurBmp = value

End Set

End Property

 

Private mCurHobject As HObject

Public Property CurHobject() As HObject

Get

Return mCurHobject

End Get

Set(ByVal value As HObject)

If mCurHobject IsNot Nothing Then

CurHobject.Dispose()

End If

mCurHobject = value

End Set

End Property

 

 

标签:End,变量,49,Dispose,Halcon,HObject,内存,HOperatorSet,HImg
来源: https://www.cnblogs.com/ihh2021/p/16345647.html