【发布时间】:2014-07-01 00:01:48
【问题描述】:
我正在开发一个连接到 GigEVision 相机并从中提取图像的应用程序。我目前正在使用带有 C#.NET 的 Pleora eBus SDK。
下面的代码只是一个用于相机连接的测试应用程序 - 它可以流式传输图像,但除非我调用 GC.Collect(); 否则会很快耗尽内存; 值得注意的是,流式传输的图像很大 (4096x3072),因此崩溃发生得相当快。
起初我怀疑不调用 Dispose() 是问题所在。但是,我可以在删除对每个图像的引用之前对每个图像调用 Dispose(),但这并不能解决问题。
我也尝试过显式释放进入显示线程回调的缓冲区,但没有效果。
我可以用更优雅的方式找回我的记忆吗?
using System;
using System.Windows.Forms;
using PvDotNet;
using PvGUIDotNet;
using System.Drawing;
namespace eBus_Connection
{
public partial class MainForm : Form
{
PvDeviceGEV camera;
PvStreamGEV stream;
PvPipeline pipeline;
PvDisplayThread thread;
bool updating = false;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Shown(object sender, EventArgs e)
{
PvDeviceInfo info;
PvDeviceFinderForm form = new PvDeviceFinderForm();
form.ShowDialog();
info = form.Selected;
camera = PvDeviceGEV.CreateAndConnect(info) as PvDeviceGEV;
stream = PvStreamGEV.CreateAndOpen(info.ConnectionID) as PvStreamGEV;
pipeline = new PvPipeline(stream);
if (camera == null || stream == null)
throw new Exception("Camera or stream could not be created.");
camera.NegotiatePacketSize();
camera.SetStreamDestination(stream.LocalIPAddress, stream.LocalPort);
camera.StreamEnable();
camera.Parameters.ExecuteCommand("AcquisitionStart");
pipeline.Start();
thread = new PvDisplayThread();
thread.OnBufferDisplay += thread_OnBufferDisplay;
thread.Start(pipeline, camera.Parameters);
status.DisplayThread = thread;
status.Stream = stream;
}
void thread_OnBufferDisplay(PvDisplayThread aDisplayThread, PvBuffer aBuffer)
{
Bitmap b = new Bitmap((int)aBuffer.Image.Width, (int)aBuffer.Image.Height);
aBuffer.Image.CopyToBitmap(b);
BeginInvoke(new Action<Bitmap>(ChangeImage), b);
}
void ChangeImage(Bitmap b)
{
if (PictureBox.Image != null)
PictureBox.Dispose();
PictureBox.Image = b;
GC.Collect();//taking this away causes memory to leak rapidly.
}
}
}
【问题讨论】:
-
你需要打电话给
PictureBox.Image.Dispose(),而不是PictureBox.Dispose()。 -
@MichaelLiu 你是对的。谢谢。