我正在使用sdk的演示版(只是一个已编译的dll)在Windows应用中使用网络摄像头读取条形码。
当我完成表单时,我无法释放任何内存。在表单打开时,它将继续占用更多内存,直到关闭。关闭时不会释放任何内存。当我再次打开相同的表格时,它开始吞噬更多的内存直到关闭。此过程一直持续到没有更多可用内存,然后主应用程序崩溃。
barcode.dll显然存在无法释放资源的问题。但是,我不应该能够杀死我引用的项目和冒犯dll以使它释放它不会放弃的任何内存吗?
我试过了:
谁能告诉我我做错了什么或给我任何想法?我希望我发布了足够的信息。
以下是代码:
originalForm.vb(在主项目中)
Dim frm As New Library.Helpers.frmWebcamScanner(True)
If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then txtVIN.Text = frm.BarcodeReading
frm.Dispose()
然后我在frm.Dispose()行之后添加了这个:
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
secondForm.cs(在引用的项目中)
namespace Library.Helpers
{
public partial class frmWebcamScanner : Form
{
private bool _ScanningForVIN = false;
/// <summary>
/// The results of the scan
/// </summary>
public string BarcodeReading = "";
/// <summary>
///
/// </summary>
/// <param name="ScanningForVIN">Are you trying to read a VIN? t/f</param>
public frmWebcamScanner(bool ScanningForVIN)
{
_ScanningForVIN = ScanningForVIN;
InitializeComponent();
}
private void frmWebcamScanner_Load(object sender, EventArgs e)
{
this.Top = this.Top + 10;
barcodeReader1.Start();
timer1.Start();
}
private void barcodeReader1_BarcodeReady(object sender, BarcodeEvent e)
{
BarcodeReading = e.barcode.barcode;
if (_ScanningForVIN)
{
//Remove the leading *I if there:
BarcodeReading = BarcodeReading.Replace("*I", "");
//Remove all asterisks:
BarcodeReading = BarcodeReading.Replace("*", "");
}
this.DialogResult = System.Windows.Forms.DialogResult.OK;
PrepAndClose();
}
private void frmWebcamScanner_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
PrepAndClose();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Abort;
PrepAndClose();
}
private void PrepAndClose()
{
timer1.Stop();
barcodeReader1.Stop();
this.Close();
}
}
}
然后我定制了designer.cs处理代码,试图释放所有内容以使垃圾收集器启动。我在这里设置了一个断点并验证一切正常。
来自designer.cs:
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
barcodeReader1.Stop();
timer1.Stop();
this.Load -= this.frmWebcamScanner_Load;
this.KeyDown -= this.frmWebcamScanner_KeyDown;
this.barcodeReader1.BarcodeReady -= this.barcodeReader1_BarcodeReady;
this.timer1.Tick -= this.timer1_Tick;
barcodeReader1.Dispose();
barcodeReader1 = null;
components.Dispose();
}
base.Dispose(disposing);
}