【问题标题】:OpenCV Weird Memory CorruptionOpenCV 奇怪的内存损坏
【发布时间】:2016-12-30 14:43:52
【问题描述】:

我用 Visual Studio 为一个项目设置了 OpenCV,但我遇到了这些非常奇怪的内存错误。我一直在广泛寻找解决此问题的方法,虽然有许多类似的问题,但它们要么没有答案,要么对我不起作用。

这是少数几个我遇到问题的 OpenCV 函数之一(从 docs 获得),它复制了我得到的错误:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);


/** @function main */
int main(int argc, char** argv)
{
    /// Load source image and convert it to gray
    std::string img = "<path-to-picture>";
    src = imread(img, CV_LOAD_IMAGE_COLOR);

    /// Convert image to gray and blur it
    cvtColor(src, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));

    /// Create Window
    char* source_window = "Source";
    namedWindow(source_window, CV_WINDOW_AUTOSIZE);
    imshow(source_window, src);

    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using canny
    Canny(src_gray, canny_output, thresh, thresh * 2, 3);
    /// Find contours
    findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Draw contours
    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
    }

    /// Show in a window
    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours", drawing);


    waitKey(0);
    return(0);
}

奇怪的是 findContours() 运行良好,但之后程序崩溃并出现此错误

Expression: "(_Ptr_user & (_BIG_ALLOCATION_ALIGNMENT - 1)) == 0" && 0

关于如何解决这个问题的任何想法?这是我的 OpenCV 设置:

  • Visual Studio 2015,调试/发布 x64
  • OpenCV 2.4.13(预建)
  • C++ 包含 指向build\include
  • C++ 链接器 指向\build\x64\vc12\lib
  • 依赖项包括上述文件夹中的库。

【问题讨论】:

  • 首先,按下 Retry 并跟踪调用堆栈,直到到达您自己的代码以找出哪个 OpenCV 函数失败。
  • @molbdnilo 在我的项目代码中,我评估了堆栈跟踪 multiple 次,我确信 findContours() 失败了。如果您愿意,我也可以添加该堆栈跟踪。
  • 那么,哪一个是真的? “findContours() 完美运行”或“我确定 findContours() 失败了”?
  • @molbdnilo 奇怪的是 findContours 工作,但是在我传递给它的向量超出范围后,它崩溃了,堆栈跟踪显示它在向量的释放器处崩溃

标签: c++ visual-studio opencv memory-leaks runtime-error


【解决方案1】:

您正在使用带有 vc12 编译器 (Visual Studio 2013) 的 OpenCV 构建,但在您的项目中您使用的是 vc14 (Visual Studio 2105)。

请务必使用使用 vc14 编译的预编译库。

我确定 OpenCV 3.1 具有 vc14 的预构建二进制文件。我不知道 OpenCV 2.4.13 是否也有它们(可能没有)。这种情况你需要用vc14重新编译OpenCV,或者切换到OpenCV 3.1

【讨论】:

  • 我自己从未构建过 OpenCV,所以我希望有一个更简单的解决方案 :(
  • 然后下载opencv 3.1,或者使用visual studio 2013 ;D
  • 原来在 VC14 上构建 2.4.13 失败可怕。最终在 VC12 上构建它,因为我需要 OpenMP 和 CUDA。
猜你喜欢
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多