第一次使用 EmguCV 处理图像问题,使用过程中总是莫名其妙的闪退,程序崩溃,或者内存泄漏。
刚开始以为是EmguCV那些地方用的不对,后来发现可能更是因为 PictureBox用的不好。
罗列注意事项一两点。希望能保住遇到同样问题的童鞋们。
EmguCV 是 OpenCV在.Net平台上的一个包,详见 http://www.emgu.com/wiki/index.php/Main_Page
我用的EmguCV 版本是 NuGet中的v3.4.3.3016
故障1:
“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
An unhandled exception of type 'System.AccessViolationException' occurred in System.Drawing.dll
多半因为没有及时Dispose掉不用的缓存,或者持续引用导致GC都清理不掉部分缓存。
故障2:
程序闪退
多半因为调用EmguCV时出错,建议在适当的地方添加 Try Catch.
收获 和 注意事项
1.频繁设置PictureBox时,释放上一次缓存。
(参考:https://stackoverflow.com/questions/1831732/c-sharp-picturebox-memory-releasing-problem)
1 public static void ClearAndSetImage(PictureBox dest, Bitmap src) 2