问题
?windows中 C++? 调用OpenCV的findContours函数时,会出现崩溃问题
分析
?弹框直接报内存方面的严重异常错误。
vector<Vec4i>hierarchy;//
vector<vector<Point>>contours;//轮廓数组
findContours(Dst, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
就是上述几行代码,跟踪代码,发现findContours 是可以执行的,在包含这几段函数的执行结束时弹框异常。凭借直觉分析,是出现在析构函数上的错误。下面的英文资料可以借助分析
Change your project settings to use the non-DLL version of the runtime library. You will be using two different heap managers -- you can't create a pointer from one heap manager and delete it with another heap manager. Your program will crash, if not now, eventually it will.? This is a well-known problem for years, and has been written about in various articles, most notably in the C/C++ User's Journal a couple of years ago when discussing VC++ and STL (Sorry, I can't find the article).?
大概就是说如果设置为非DLL的库,管理动态生成的内存空间就会冲突。这个冲突就是:vector<vector<Point>>contours 生命期结束时执行析构函数去释放findContours 申请的动态空间导致的。
解决方法1:网上很多帖子也有,C/C++>代码生成->运行库->多线程DLL(/MD)
解决方法2:新建一个多线程DLL(/MD) 的库,findContours函数放在这个DLL里面。适合当前DLL无法设置为多线程DLL(/MD)的情况。
|