【VS開發】vector features(num_images)析構報錯

代碼一鼓作氣,但運行的時候會出現_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路徑中了!!!

相關文章
相關標籤/搜索