【发布时间】: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