opencv报错 断言 __acrt_first_block == header

问题描述

在一个使用opencv的项目里,把MDD改成了MTD之后,Debug模式遇到了错误。

debug_heap.cpp 第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);

解决方案

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();
}

添加完后运行还是有错, 找到我们的业务代码. 把其中的这个改为那个:

std::vector<cv::Mat> chs;                //原来为
std::vector<cv::Mat> chs(m.channels());  //改为

可以成功运行了.

上一篇:JSP 的 4 种作用域?


下一篇:获取url参数值(可解码中文值)