Visual Studio C# 试图读取或写入受保护的内存。这通常表明其他内存已损坏
Posted
技术标签:
【中文标题】Visual Studio C# 试图读取或写入受保护的内存。这通常表明其他内存已损坏【英文标题】:Visual Studio C# Attempted to read or write protected memory. This is often an indication that other memory is corrupt 【发布时间】:2018-02-19 04:24:21 【问题描述】:我正在创建一个使用 4 个摄像头进行面部识别的考勤系统。我在 C# 中使用 Emgu CV 3.0。现在,在我的由 4 个图像框组成的考勤表单中,应用程序突然停止并返回主表单,并在引用考勤表单的按钮上显示错误。错误是:
试图读取或写入受保护的内存。这通常表明其他内存已损坏。
这是发生错误的代码:
private void btn_attendance_Click(object sender, EventArgs e)
attendance attendance = new attendance();
attendance.ShowDialog();
这是不带识别部分的考勤表代码:
public partial class attendance : Form
private Capture cam1, cam2, cam3, cam4;
private CascadeClassifier _cascadeClassifier;
private RecognizerEngine _recognizerEngine;
private String _trainerDataPath = "\\traineddata_v2";
private readonly String dbpath = "Server=localhost;Database=faculty_attendance_system;Uid=root;Pwd=root;";
mysqlConnection conn;
public attendance()
InitializeComponent();
conn = new MySqlConnection("Server=localhost;Database=faculty_attendance_system;Uid=root;Pwd=root;");
private void btn_home_Click(object sender, EventArgs e)
this.Close();
private void attendance_Load(object sender, EventArgs e)
time_now.Start();
lbl_date.Text = DateTime.Now.ToString("");
_recognizerEngine = new RecognizerEngine(dbpath, _trainerDataPath);
_cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_default.xml");
cam1 = new Capture(0);
cam2 = new Capture(1);
cam3 = new Capture(3);
cam4 = new Capture(4);
Application.Idle += new EventHandler(ProcessFrame);
private void ProcessFrame(Object sender, EventArgs args)
Image<Bgr, byte> nextFrame_cam1 = cam1.QueryFrame().ToImage<Bgr, Byte>();
Image<Bgr, byte> nextFrame_cam2 = cam2.QueryFrame().ToImage<Bgr, Byte>();
Image<Bgr, byte> nextFrame_cam3 = cam3.QueryFrame().ToImage<Bgr, Byte>();
Image<Bgr, byte> nextFrame_cam4 = cam4.QueryFrame().ToImage<Bgr, Byte>();
using (nextFrame_cam1)
if (nextFrame_cam1 != null)
Image<Gray, byte> grayframe = nextFrame_cam1.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam1.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam1.Bitmap));
imageBox1.Image = nextFrame_cam1;
using (nextFrame_cam2)
if (nextFrame_cam2!= null)
Image<Gray, byte> grayframe = nextFrame_cam2.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam2.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam2.Bitmap));
imageBox2.Image = nextFrame_cam2;
using (nextFrame_cam3)
if (nextFrame_cam3!= null)
Image<Gray, byte> grayframe = nextFrame_cam3.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam3.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam3.Bitmap));
imageBox3.Image = nextFrame_cam3;
using (nextFrame_cam4)
if (nextFrame_cam4!= null)
Image<Gray, byte> grayframe = nextFrame_cam4.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam4.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam4.Bitmap));
imageBox4.Image = nextFrame_cam4;
【问题讨论】:
所以使用调试器并单步执行代码来找出问题的实际位置。我们不能为您做到这一点;我们没有您所有的代码和项目文件和参考资料。 【参考方案1】:请阅读这篇文章以了解什么是内存泄漏。 http://www.dotnetfunda.com/articles/show/625/best-practices-no-5-detecting-net-application-memory-leaks
您的错误表明您正在创建一个类的许多实例或任何函数的递归调用。 使用 Using() 创建 Emgu 的对象,以便在您的代码终止后立即释放托管或非托管内存。
public partial class attendance : Form
private Capture cam1, cam2, cam3, cam4;
private CascadeClassifier _cascadeClassifier;
private RecognizerEngine _recognizerEngine;
private String _trainerDataPath = "\\traineddata_v2";
private readonly String dbpath = "Server=localhost;Database=faculty_attendance_system;Uid=root;Pwd=root;";
MySqlConnection conn;
public attendance()
InitializeComponent();
conn = new MySqlConnection("Server=localhost;Database=faculty_attendance_system;Uid=root;Pwd=root;");
private void btn_home_Click(object sender, EventArgs e)
this.Close();
private void attendance_Load(object sender, EventArgs e)
time_now.Start();
lbl_date.Text = DateTime.Now.ToString("");
_recognizerEngine = new RecognizerEngine(dbpath, _trainerDataPath);
_cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_default.xml");
cam1 = new Capture(0);
cam2 = new Capture(1);
cam3 = new Capture(3);
cam4 = new Capture(4);
Application.Idle += new EventHandler(ProcessFrame);
private void ProcessFrame(Object sender, EventArgs args)
using (Image<Bgr, byte> nextFrame_cam1 = cam1.QueryFrame().ToImage<Bgr, Byte>())
if (nextFrame_cam1 != null)
Image<Gray, byte> grayframe = nextFrame_cam1.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam1.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam1.Bitmap));
imageBox1.Image = nextFrame_cam1;
using (Image<Bgr, byte> nextFrame_cam2 = cam2.QueryFrame().ToImage<Bgr, Byte>())
if (nextFrame_cam2 != null)
Image<Gray, byte> grayframe = nextFrame_cam2.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam2.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam2.Bitmap));
imageBox2.Image = nextFrame_cam2;
using (Image<Bgr, byte> nextFrame_cam3 = cam3.QueryFrame().ToImage<Bgr, Byte>())
if (nextFrame_cam3 != null)
Image<Gray, byte> grayframe = nextFrame_cam3.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam3.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam3.Bitmap));
imageBox3.Image = nextFrame_cam3;
using (Image<Bgr, byte> nextFrame_cam4 = cam4.QueryFrame().ToImage<Bgr, Byte>())
if (nextFrame_cam4 != null)
Image<Gray, byte> grayframe = nextFrame_cam4.Convert<Gray, byte>();
var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
foreach (var face in faces)
nextFrame_cam4.Draw(face, new Bgr(Color.Green), 3);
var predictedUserId = _recognizerEngine.RecognizeUser(new Image<Gray, byte>(nextFrame_cam4.Bitmap));
imageBox4.Image = nextFrame_cam4;
请按照本文档了解使用 EMGU.CV 进行人脸识别的标准方法。 http://www.emgu.com/wiki/index.php/Face_detection
【讨论】:
欢迎来到 SO。如果可能,最好引用链接中最重要的部分(同时保留链接作为参考)。这样你的答案在未来几年就不会过时。谢谢 感谢@LonelyNeuron以上是关于Visual Studio C# 试图读取或写入受保护的内存。这通常表明其他内存已损坏的主要内容,如果未能解决你的问题,请参考以下文章
“试图读取或写入受保护的内存。这通常表明其他内存已损坏” DllImporting C#
c# emgu/opencv 使用抛出异常 - 试图读取或写入受保护的内存
C++ DLL 到 C# 错误:“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”
C 和 System.AccessViolationException 中的 allocVector():试图读取或写入受保护的内存?