redpranger
redpranger

Reputation: 21

Visual Studio C# Attempted to read or write protected memory. This is often an indication that other memory is corrupt

I am creating an Attendance System using 4 cameras for facial recognition. I am using Emgu CV 3.0 in C#. Now, in my attendance form, which consist of 4 imagebox, the application suddenly stops and it goes back to the main form and shows an error to the button which reference the attendance form. The error was:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Here is the code where the error occured:

    private void btn_attendance_Click(object sender, EventArgs e)
    {
        attendance attendance = new attendance();
        attendance.ShowDialog();
    }

Here is the code for the Attendance form without the recognition part:

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;
            }
        }
    }
}

Upvotes: 1

Views: 16097

Answers (1)

habib
habib

Reputation: 2446

Plz read this post to know what is memory leakage. http://www.dotnetfunda.com/articles/show/625/best-practices-no-5-detecting-net-application-memory-leaks

Your error indicate that you are creating many instances of a class or any recursive call of function. Use Using() to create object of Emgu so that as soon as your code terminate the managed or unmanaged memory will be disposed.

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;
            }
        }
    }
}

Plz fowllow this document for standard way to work with EMGU.CV for face recongnization. http://www.emgu.com/wiki/index.php/Face_detection

Upvotes: 1

Related Questions