3

I am using following code for background subtraction. I am giving it path of video, video runs successfully but at the end it gives Debug Assertion Failed error.

enter image description here

I am using following code in Microsoft Visual Studio to solve a problem of Computer Vision with opencv.

#include<opencv2/opencv.hpp>
#include<iostream>
#include<string>
#include<vector>    
#include "opencv2/video/background_segm.hpp"
using namespace cv;
using namespace std;

int main()
{   
    Mat frame;
    Mat back;
    Mat fore;

    VideoCapture cap;
    cap.open("H:/competition.avi");

    BackgroundSubtractorMOG2 bg(100,16,true);    
    bg.set("nmixtures",3);

    vector<vector<Point> > contours;

    namedWindow("Frame");
    namedWindow("Background");

    for(;;)
    {
        cap >> frame;
        if(!frame.empty())
        {
            bg.operator ()(frame,fore);
            bg.getBackgroundImage(back);
            erode(fore,fore,Mat());
            dilate(fore,fore,Mat());
            findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
            drawContours(frame,contours,-1,Scalar(0,0,255),2);
            imshow("Frame",frame);
            imshow("Background",back);
            if(waitKey(30) >= 0) break;
        }
        else
            break;
    }
    return 0;
}
Hammad Hassan
  • 1,192
  • 17
  • 29
  • Did you try to debug? Even if the crash happens in a system or library file, you can walk up the call-stack to your code to see where it happens. – Some programmer dude Nov 07 '14 at 15:00
  • 3
    Make sure you're linking to the correct libraries. Building in debug mode and linking to release mode libraries can cause this type of problem. – Captain Obvlious Nov 07 '14 at 15:02
  • @CaptainObvlious I have checked. All the libraries are linked correctly in Debug and Release Mode. – Hammad Hassan Nov 07 '14 at 15:08
  • @JoachimPileborg I tried to debug it and in the library file it was stopped at the line _pFirstBlock == pHead . – Hammad Hassan Nov 07 '14 at 15:10
  • Possible duplicate of [Debug Assertion Failed! Expression: \_pFirstBlock == pHead](https://stackoverflow.com/questions/18882760/debug-assertion-failed-expression-pfirstblock-phead) – Roalt Oct 30 '17 at 14:50

5 Answers5

4

I just came across this issue and after serious web trawling, found the answer, at least it worked in my case...

you need to go to your visual studio project settings, to c/c++, to code generation and change the runtime library to Multi-threaded Debug DLL (/MDd).

It seems this error is from a single threaded DLL trying to run in a multi thread project, or something of that nature.

good luck!

anti
  • 3,011
  • 7
  • 36
  • 86
1

For unknown reasons, some versions of opencv (2.x at least) have a CMake variable "BUILD_WITH_STATIC_CRT" that by default gets set to on, thus causing issues like that. Disable that flag, then the solution should get generated with /MDd defined.

Secondarily, open your exe file in dependency walker. Look for multiple versions of MS C++ runtime libraries. For example, you may have a version of QT built against msvcp110.dll (visual studio 2012) but your current project uses msvcp120.dll (visual studio 2013).

peter karasev
  • 2,578
  • 1
  • 28
  • 38
0

Alright. First thing first: Hit Retry, assuming that you are debugging (F5), and have not launched (Run) the program by hitting (Ctrl+F5). As soon as you'd hit Retry, you will see call stack in debugger.

The call stack will give you possible hint where that invalid/double free/delete is happening. That would be your starting point to analyse the issue. See if some memory is double freed, allocated using different heap (for example, with malloc, and being deleted). Or, if memory allocated by VC9 (for example), is being freed by a DLL written in VC8.

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • Please see call stack, and find out which function is calling that routine. – Ajay Nov 07 '14 at 15:29
  • Debug is stopped at this line in dbgheap.c file. _ASSERTE(_pFirstBlock == pHead) and call stack is not helpful in this case as expected because _pFirstBlock and pHead contains memory references. – Hammad Hassan Nov 07 '14 at 15:31
  • You need to give some efforts. Learn debugging. – Ajay Nov 07 '14 at 15:32
0

I had the same error,

File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c Line 1424

Expression:_pFirstBlock == pHead

when using debug mode on vs12 when testing opencv code for augmented reality, for reference the code I used was from here.

The Solution that worked for me: The problem went away for me after I updated the visual studio settings for release mode, even though I was only using debug. Other opencv code runs no problem in debug mode, so I had not bothered to fully configured the release settings.

Anyway specifically in release the parts I had to update were in Properties -> C++ -> Additional Include Directories; and Properties -> Linker -> Input -> Additional Dependencies. After that the code ran error free in debug mode and release mode. If you don't know what settings to use, they are listed in the setup instruction pages on the opencv website, vs12 instructions are here

sinkpoh
  • 1
  • 3
0

I meet the same problem. I find the resolution through this URL. Debug Assertion Failed Expression: _pFirstBlock == pHead using OpenCV and C++ trying to call SurfFeatureDetector

The reason for this error is the configuration problem,vs2012 is matched with vc11 folder. This may help you.