how to find out what is causing “cv::Exception at

2020-07-06 02:22发布

问题:

I'm currently suffering from some strange exceptions that are most probably due to me doing something incorrectly while interacting with opencv:

First-chance exception at 0x7580b9bc in xxx.exe: Microsoft C++ exception: cv::Exception at memory location 0x00c1c624..

I've already enabled the Thrown field in the Debug -> Exceptions menu, however I really can't figure out where in my code the exception is thrown.

How can I debug this?

EDIT the stack frame reads like this (my app won't even show up in the list!):

  • KernelBase.dll!7580b8bc()
  • [Frames below may be incorrect or missing ]
  • KernelBase.dll!7580b8bc()
  • opencv_core242d.dll!54eb60cc()

回答1:

You could wrap your entire main in a try catch block which prints out the exception details. If the open CV API can throw exceptions, you will need to think about handling them anyway as part of your design:

try
{
  // ... Contents of your main
}
catch ( cv::Exception & e )
{
 cerr << e.msg << endl; // output exception message
}


回答2:

OpenCV has this handy function called cv::setBreakOnError

If you put the following into your main before any opencv calls:

cv::setBreakOnError(true);

then your program will crash, because OpenCV will do an invalid operation (dereferencing a null pointer) just before it would throw cv::Exception normally. If you run your code in a debugger, it will stop at this illegal operation, and you can see the whole call stack with all of your codes and variables at the time of the error.



回答3:

I´ve got this problem by using OpenCV with WebCam. The problem in my case is that the program is trying to read an image when the Cam hasn't been initialized.

my error code:

 // open camera
capture.open(0);
while (1){
    //store image to matrix // here is the bug
    capture.read(cameraFeed);

The solution

 // open camera
capture.open(0);
while (1){

     //this line makes the program wait for an image 
     while (!capture.read(cameraFeed));

    //store image to matrix 
    capture.read(cameraFeed);

(sorry about my english) Thanks