[Solved] Opencv error: assertion__acrt_first_block == header

Problem description

In a project using OpenCV, after changing MDD to MTD, the debug mode encountered an error.

debug_heap.cpp   Line 996

        if (header->_block_header_prev)
        {
            header->_block_header_prev->_block_header_next = header->_block_header_next;
        }
        else
        {
            _ASSERTE(__acrt_first_block == header);            //# 996 LINE
            __acrt_first_block = header->_block_header_next;
        }

        memset(header, dead_land_fill, sizeof(_CrtMemBlockHeader) + header->_data_size + no_mans_land_size);
        _free_base(header);

Solution

Add the following functions to region.cpp

void findContours(const cv::Mat &src, std::vector<std::vector<cv::Point>> &contours, std::vector<cv::Vec4i> &hierarchy, int retr = cv::RETR_LIST, int method = cv::CHAIN_APPROX_SIMPLE, cv::Point offset = cv::Point(0, 0))
{
    using namespace cv;
    CvMat c_image = src;
    MemStorage storage(cvCreateMemStorage());
    CvSeq *_ccontours = 0;
    cvFindContours(&c_image, storage, &_ccontours, sizeof(CvContour), retr, method, CvPoint(offset));

    if (!_ccontours)
    {
        contours.clear();
        return;
    }
    Seq<CvSeq *> all_contours(cvTreeToNodeSeq(_ccontours, sizeof(CvSeq), storage));
    int total = (int)all_contours.size();
    contours.resize(total);

    SeqIterator<CvSeq *> it = all_contours.begin();
    for (int i = 0; i < total; i++, ++it)
    {
        CvSeq *c = *it;
        ((CvContour *)c)->color = (int)i;
        int count = (int)c->total;
        int *data = new int[count * 2];
        cvCvtSeqToArray(c, data);
        for (int j = 0; j < count; j++)
        {
            contours[i].push_back(Point(data[j * 2], data[j * 2 + 1]));
        }
        delete[] data;
    }

    hierarchy.resize(total);
    it = all_contours.begin();
    for (int i = 0; i < total; i++, ++it)
    {
        CvSeq *c = *it;
        int h_next = c->h_next ?((CvContour *)c->h_next)->color : -1;
        int h_prev = c->h_prev ?((CvContour *)c->h_prev)->color : -1;
        int v_next = c->v_next ?((CvContour *)c->v_next)->color : -1;
        int v_prev = c->v_prev ?((CvContour *)c->v_prev)->color : -1;
        hierarchy[i] = Vec4i(h_next, h_prev, v_next, v_prev);
    }
    storage.release();
}

There are still errors after adding,   Find our business code   Change this one to that one:

std::vector<cv::Mat> chs;                //BEFORE
std::vector<cv::Mat> chs(m.channels());  //NEW

It can run successfully

Read More: