如何在内存位置找出导致" cv ::异常的原因"?

时间:2012-10-02 09:23:40

标签: c++ visual-studio-2010 visual-c++

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

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

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

我该如何调试?

修改 堆栈框架如下所示(我的应用程序甚至不会显示在列表中!):

  • KernelBase.dll!7580b8bc()
  • [下面的框架可能不正确或缺失]
  • KernelBase.dll!7580b8bc()
  • opencv_core242d.dll!54eb60cc()

3 个答案:

答案 0 :(得分:13)

您可以将整个main包装在try catch块中,该块打印出异常详细信息。如果开放的CV API可以抛出异常,那么无论如何都需要考虑处理它们:

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

答案 1 :(得分:5)

OpenCV有一个名为cv::setBreakOnError

的便利功能

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

cv::setBreakOnError(true);

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

答案 2 :(得分:2)

我通过将OpenCV与WebCam一起使用来解决这个问题。我的问题是,当Cam尚未初始化时,程序正在尝试读取图像。

我的错误代码:

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

(抱歉我的英文) 感谢