EmguCV尝试读取或写入受保护的内存

Moa*_*med 2 c# exception-handling exception emgucv

我有以下代码使用EmgucV在imagebox中显示图像:

    Capture capture;
    Image<Bgr, Byte> image;

    public Form1()
    {
        InitializeComponent();
        Application.Idle += new EventHandler(Start);
    }
    void Start(object sender, EventArgs e)
    {
        capture = new Capture();
        image = capture.QueryFrame();
        imageBox1.Image = image;
    }
Run Code Online (Sandbox Code Playgroud)

我得到了例外Attempted to read or write protected memory.我需要做些什么才能纠正这个问题?

sur*_*fen 5

这表明可能存在本机内存泄漏

我认为您的代码中存在错误.Start在应用程序生命周期中,您的方法将被多次调用(经常).

看起来您应该在应用程序中只使用一个Capture对象.

只需将Capture实例移动到Form构造函数:

Capture capture;

public Form1()
{
    InitializeComponent();
    Application.Idle += new EventHandler(Capture);
    capture = new Capture();
}
void Capture(object sender, EventArgs e)
{
    imageBox1.Image = capture.QueryFrame(); 
}
Run Code Online (Sandbox Code Playgroud)