如何找出导致"cv::Exception at memory location"的原因?

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

本文关键字:location memory Exception 何找出 cv at      更新时间:2023-10-16

我目前遇到了一些奇怪的异常,很可能是由于我在与opencv交互时做错了什么:

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

我已经在Debug -> Exceptions菜单中启用了Thrown字段,但是我真的无法弄清楚异常在我的代码中的位置。

我该如何调试?

编辑堆栈帧如下所示(我的应用程序甚至不会出现在列表中!

  • KernelBase.dll!7580b8bc()
  • [以下帧可能不正确或缺失]
  • KernelBase.dll!7580b8bc()
  • opencv_core242d.dll!54eb60cc()

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

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

OpenCV 有一个名为 cv::setBreakOnError 的方便函数

如果您在任何 opencv 调用之前将以下内容放入您的主内容:

cv::setBreakOnError(true);

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

我通过使用OpenCV和WebCam遇到了这个问题。就我而言,问题是程序在凸轮尚未初始化时尝试读取图像。

我的错误代码:

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

解决方案

 // 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);

(对不起我的英语)谢谢