代码一鼓作气,但运行的时候会出现_CrtIsValidHeapPointer的异常,跟进去调了一上午的Bug,终于搞定css
跟踪定位到 _CrtIsValidHeapPointer ,注意到 g 8h"@dbgheap.c 文件中 _CrtIsValidHeapPointer 处注释:
/*
* If this ASSERT fails, a bad pointer has been passed in. It may be
* totally bogus, or it may have been allocated from another heap.
* The pointer MUST come from the 'local' heap.
*/
_ASSERTE(_CrtIsValidHeapPointer(pUserData));
大概是由于 dll 若是静态连接了运行时库,dll 就会拥有独立于应用程序堆(也称做local heap)的运行时堆实例。此时在 dll 外部就不能访问此 local heap,因此也就有上面所出现的异常啦。MSDN 中也有介绍:
The _CrtIsValidHeapPointer function is used to ensure that a specific memory address is within the local heap. The local heap refers to the heap created and managed by a particular instance of the C run-time library. If a dynamic-link library (DLL) contains a static link to the run-time library, it has its own instance of the run-time heap, and therefore its own heap, independent of the application's local heap. When _DEBUG is not defined, calls to _CrtIsValidHeapPointer are removed during preprocessing.
html
程序崩溃在当析构一个带有vector成员函数对象的时候,在析构vector时,会出现这个错误,大体缘由是由于析构的时候找不到vector分配的空间app
一行一行查看代码发现,对象里面的points2, status等vector变量是在calcOpticalFlowPyrLK(img1, img2, points1, points2, status, similarity, window_size, level, term_criteria, lambda, 0); 函数中分配的,即opencv的dll,因此当对象进行析构的时候,由于不能访问此local heap因此会有异常崩溃。函数
解决方法:this
在调用opencv的函数以前,本身进行空间的分配spa
还有一种多是由于VS版本和opencv使用的版本不一致形成,在用hog进行行人检测的时候,出现的便是这个问题调试
我在调试vector<ImageFeatures> features(num_images);析构的时候也出现了如此问题!htm
最后经过修改PATH路径中引用的opencv的VC版本为VC12(原来是VC14)才正常运行结束,这种诡异的问题实在是耽误事儿!!!对象
还得注意的是后面基于VS2015开发的程序再运行的时候可能也会出现问题,到时候别再搞的莫名其妙。ci
最好的办法仍是直接拷贝dll到对应的可执行文件路径,不要添加到PATH路径中了!!!