C++ 如何找出导致“cv::Exception at memory location”的原因?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12687684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:31:45  来源:igfitidea点击:

how to find out what is causing "cv::Exception at memory location"?

c++visual-studio-2010visual-c++

提问by memyself

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

我目前正在遭受一些奇怪的异常,这很可能是由于我在与 opencv 交互时做错了一些事情:

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

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

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

我已经启用了菜单中的Thrown字段Debug -> Exceptions,但是我真的不知道在我的代码中抛出异常的位置。

How can I debug this?

我该如何调试?

EDITthe 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()
  • KernelBase.dll!7580b8bc()
  • [以下框架可能不正确或缺失]
  • KernelBase.dll!7580b8bc()
  • opencv_core242d.dll!54eb60cc()

回答by Benj

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:

您可以将整个 main 包装在一个 try catch 块中,该块打印出异常详细信息。如果开放 CV API 可以抛出异常,您将需要考虑将它们作为设计的一部分进行处理:

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

回答by tr3w

OpenCV has this handy function called cv::setBreakOnError

OpenCV 有一个方便的函数,叫做cv::setBreakOnError

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

如果在任何 opencv 调用之前将以下内容放入 main 中:

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.

那么您的程序将崩溃,因为 OpenCV 会在正常抛出 cv::Exception 之前执行无效操作(取消引用空指针)。如果您在调试器中运行您的代码,它会在这个非法操作处停止,您可以看到整个调用堆栈以及发生错误时的所有代码和变量。

回答by Bob

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.

我通过使用带有 WebCam 的 OpenCV 遇到了这个问题。在我的情况下,问题是程序试图在凸轮尚未初始化时读取图像。

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

(对不起我的英语)谢谢