7888
7888

Reputation: 72

OpenCv - cv::Exception at memory location C++

I am trying to code an openCV program that opens a new widow that displays video from the webcam (laptop), and recieve the error Unhandled exception at 0x7530C41F in cv.exe: Microsoft C++ exception: cv::Exception at memory location 0x001FF5D0.

i have double checked all of the included dll's as well as the system path without any positive results.

here is my code:

#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;
int main()
{

    Mat image;

    VideoCapture cap;
    cap.open(0);

    namedWindow("window", CV_WINDOW_AUTOSIZE);

    while (1)
    {
        cap >> image;
        imshow("window", image);
        waitKey(33);
    }
    return 0;
}

Upvotes: 2

Views: 13041

Answers (2)

user4135021
user4135021

Reputation:

Perhaps using VideoCapture::isOpened() would tell you if it's safe to use your VideoCapture instance?

Upvotes: 0

7888
7888

Reputation: 72

I discovered the problem with my code, i had to put a hold on the execution of the program using waitKey(1000) or else the program will cause a memory leak. This must have been done because the connection to the webcam on my laptop was not fully established before executing the code. Putting a wait on the code execution prevents the crash and everything is now up and running.

  #include <opencv\cv.h>
    #include <opencv\highgui.h>
    using namespace cv;
    int main()
    {

        Mat image;

        VideoCapture cap;
        cap.open(0);

        namedWindow("window", CV_WINDOW_AUTOSIZE);
            waitKey(1000);

        while (1)
        {
            cap >> image;
            imshow("window", image);
            waitKey(33);
        }
        return 0;
    }

Upvotes: 2

Related Questions