Why do I get a breakpoint when I run the program, and continue to run it will indicate that the heap is damaged?

0

Brief introduction: I am writing a program about image repair, which uses Opencv4.1.1. I am using VS2019 and the program contains one .hpp file and two .cpp files.

Issue: When I run the application in the debug mode and execute some action the error is raised: the application programm has triggered a breakpoint, heap is corrupted. Like this:

Unhandled exception at 0x00007FFF121B9269 (ntdll.dll) (in guidedinpaint.exe): 0xC0000374: heap is corrupted. (0x00007FFF122227F0).

In order to find the problem, I debug step by step.I found that the error occurred when the memory was released at the end of a function.

//
// delete_scalar.cpp
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>

////////////////////////////////////////////////////////////////
// delete() Fallback Ordering
//
// +-------------+
// |delete_scalar<----+-----------------------+
// +--^----------+    |                       |
//    |               |                       |
// +--+---------+  +--+---------------+  +----+----------------+
// |delete_array|  |delete_scalar_size|  |delete_scalar_nothrow|
// +--^----^----+  +------------------+  +---------------------+
//    |    |
//    |    +-------------------+
//    |                        |
// +--+--------------+  +------+-------------+
// |delete_array_size|  |delete_array_nothrow|
// +-----------------+  +--------------------+

_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
    #ifdef _DEBUG
    _free_dbg(block, _UNKNOWN_BLOCK);
    #else
    free(block);    //There was a problem running this line of code, suggesting missing source files
    #endif
}

This is source code:

static void icvTeleaInpaintFMM(CvMat * depth, const Mat & rgb, const CvMat * f, CvMat * out, int 
range, CvMat * t, const Mat & speed, priority_queue<PointColor, vector<PointColor>, 
greater<PointColor>> & color_queue, CvMat * order) {
    int i = 0, j = 0, ii = 0, jj = 0, k, l, q;
    float dist;

    int channels = rgb.step.buf[1];
    int step = rgb.step.buf[0];
    int number = 0;
    unsigned char* data = rgb.data;

    int abd = 0;
    while (!(color_queue.empty())) {
        cout << abd++ << endl;
        PointColor temp = color_queue.top();
        ii = temp.i;
        jj = temp.j;
        color_queue.pop();

        CV_MAT_ELEM(*f, unsigned char, ii, jj) = KNOWN;

        for (q = 0; q < 4; q++) {

            if (q == 0) { i = ii - 1; j = jj; }
            else if (q == 1) { i = ii;   j = jj - 1; }
            else if (q == 2) { i = ii + 1; j = jj; }
            else if (q == 3) { i = ii;   j = jj + 1; }

            //skip if i or j are outside the image boundaries
            if ((i <= 1) || (j <= 1) || (i > t->rows - 1) || (j > t->cols - 1)) continue;

            if (CV_MAT_ELEM(*f, unsigned char, i, j) == INSIDE) {

                Mat_<float> spd = speed;
                //float speed_ij=speed.at<float>(i-1,j-1);
                float speed_ij = spd(i - 1, j - 1);
                dist = min4(FastMarching_solve(i - 1, j, i, j - 1, f, t, speed_ij),
                    FastMarching_solve(i + 1, j, i, j - 1, f, t, speed_ij),
                    FastMarching_solve(i - 1, j, i, j + 1, f, t, speed_ij),
                    FastMarching_solve(i + 1, j, i, j + 1, f, t, speed_ij));

                CV_MAT_ELEM(*t, float, i, j) = dist;

                CvPoint2D32f gradI, gradT, r;
                float Ia = 0, Jx = 0, Jy = 0, s = 1.0e-20f, w, dst, clr, sat;
                float temp_sigma_w = 2 * SIGMA_W * SIGMA_W;
                float temp_sigma_c = 2 * SIGMA_C * SIGMA_C;
                int temp_location_1 = (i - 1) * step + (j - 1) * channels;

                for (k = i - range; k <= i + range; k++) {
                    int km = k - 1 + (k == 1), kp = k - 1 - (k == t->rows - 2);
                    for (l = j - range; l <= j + range; l++) {
                        int lm = l - 1 + (l == 1), lp = l - 1 - (l == t->cols - 2);
                        if (k > 0 && l > 0 && k < t->rows - 1 && l < t->cols - 1) {
                            if ((CV_MAT_ELEM(*f, unsigned char, k, l) != INSIDE) &&
                                ((l - j) * (l - j) + (k - i) * (k - i) <= range * range))
                            {
                                r.y = (float)(i - k);
                                r.x = (float)(j - l);

                                int temp_location_2 = (k - 1) * step + (l - 1) * channels;

                                dst = exp(-(r.x * r.x + r.y * r.y) / temp_sigma_w);

                                clr = exp(-(((float)data[temp_location_1 + 0] - (float)data[temp_location_2 + 0]) * ((float)data[temp_location_1 + 0] - (float)data[temp_location_2 + 0]) +
                                    ((float)data[temp_location_1 + 1] - (float)data[temp_location_2 + 1]) * ((float)data[temp_location_1 + 1] - (float)data[temp_location_2 + 1]) +
                                    ((float)data[temp_location_1 + 2] - (float)data[temp_location_2 + 2]) * ((float)data[temp_location_1 + 2] - (float)data[temp_location_2 + 2]))
                                    / temp_sigma_c);

                                w = (float)fabs(dst * clr);

                                if (CV_MAT_ELEM(*f, unsigned char, k, l + 1) != INSIDE) {
                                    if (CV_MAT_ELEM(*f, unsigned char, k, l - 1) != INSIDE) {
                                        gradI.x = (float)((CV_MAT_ELEM(*out, unsigned char, km, lp + 1) - CV_MAT_ELEM(*out, unsigned char, km, lm - 1))) * 2.0f;
                                    }
                                    else {
                                        gradI.x = (float)((CV_MAT_ELEM(*out, unsigned char, km, lp + 1) - CV_MAT_ELEM(*out, unsigned char, km, lm)));
                                    }
                                }
                                else {
                                    if (CV_MAT_ELEM(*f, unsigned char, k, l - 1) != INSIDE) {
                                        gradI.x = (float)((CV_MAT_ELEM(*out, unsigned char, km, lp) - CV_MAT_ELEM(*out, unsigned char, km, lm - 1)));
                                    }
                                    else {
                                        gradI.x = 0;
                                    }
                                }
                                if (CV_MAT_ELEM(*f, unsigned char, k + 1, l) != INSIDE) {
                                    if (CV_MAT_ELEM(*f, unsigned char, k - 1, l) != INSIDE) {
                                        gradI.y = (float)((CV_MAT_ELEM(*out, unsigned char, kp + 1, lm) - CV_MAT_ELEM(*out, unsigned char, km - 1, lm))) * 2.0f;
                                    }
                                    else {
                                        gradI.y = (float)((CV_MAT_ELEM(*out, unsigned char, kp + 1, lm) - CV_MAT_ELEM(*out, unsigned char, km, lm)));
                                    }
                                }
                                else {
                                    if (CV_MAT_ELEM(*f, unsigned char, k - 1, l) != INSIDE) {
                                        gradI.y = (float)((CV_MAT_ELEM(*out, unsigned char, kp, lm) - CV_MAT_ELEM(*out, unsigned char, km - 1, lm)));
                                    }
                                    else {
                                        gradI.y = 0;
                                    }
                                }

                                Ia += (float)w * (float)(CV_MAT_ELEM(*out, unsigned char, km, lm));
                                Jx -= (float)w * (float)(gradI.x * r.x);
                                Jy -= (float)w * (float)(gradI.y * r.y);
                                s += w;
                            }
                        }
                    }
                }

                sat = (float)((Ia / s) + (Jx + Jy) / (sqrt(Jx * Jx + Jy * Jy) + 1.0e-20f) + 0.5f);

                int isat = cvRound(sat);

                CV_MAT_ELEM(*out, unsigned char, i - 1, j - 1) = CV_CAST_8U(isat);
                CV_MAT_ELEM(*f, unsigned char, i, j) = BAND;
                number = number + 1;
                //std::cout<<i<<","<<j<<","<<isat<<"number"<<number<<endl;
                /*if(isat < 125 )
                    std::cout<<i<<","<<j<<","<<isat<<","<<number<<endl;*/
                CV_MAT_ELEM(*order, unsigned short int, i - 1, j - 1) = number;
                color_queue.push(PointColor(i, j, dist));
            }
        }
    }
    COPY_OUT_BORDER(out);
    cout << "1" << endl;
}

void teleainpaint(const CvArr * _input_img, const CvArr * _guidance_img, const CvArr * _inpaint_mask, CvArr * _output_img, double inpaint_range, CvArr * _order)
{
    cv::Ptr<CvMat> mask, band, f, t, depth;
    cv::Ptr<IplConvKernel> el_cross, el_range;

    CvMat input_hdr, guidance_hdr, mask_hdr, output_hdr, order_hdr;
    CvMat* input_img, * guidance_img, * inpaint_mask, * output_img, * order;

    int range = cvRound(inpaint_range);
    int erows, ecols;

    input_img = cvGetMat(_input_img, &input_hdr);
    inpaint_mask = cvGetMat(_inpaint_mask, &mask_hdr);
    output_img = cvGetMat(_output_img, &output_hdr);
    guidance_img = cvGetMat(_guidance_img, &guidance_hdr);
    order = cvGetMat(_order, &order_hdr);

    //设置图片边界
    ecols = input_img->cols + 2;
    erows = input_img->rows + 2;

    //保证range在1到100之间
    range = MAX(range, 1);
    range = MIN(range, 100);

    f = cvCreateMat(erows, ecols, CV_8UC1);
    t = cvCreateMat(erows, ecols, CV_32FC1);
    band = cvCreateMat(erows, ecols, CV_8UC1);
    mask = cvCreateMat(erows, ecols, CV_8UC1);
    depth = cvCreateMat(input_img->rows, input_img->cols, CV_8UC1);

    //3 x 3 star-shaped kernel
    el_cross = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_CROSS, NULL);

    cvCopy(input_img, depth);
    cvCopy(input_img, output_img);
    cvSet(order, cvScalar(0, 0, 0, 0));
    cvSet(mask, cvScalar(KNOWN, 0, 0, 0));
    COPY_MASK_BORDER1_C1(inpaint_mask, mask); 

    SET_BORDER1_C1(mask, KNOWN);          
    cvSet(f, cvScalar(KNOWN, 0, 0, 0));     
    cvSet(t, cvScalar(1.0e6f, 0, 0, 0));    
    cvDilate(mask, band, el_cross, 1);      

    priority_queue<PointColor, vector<PointColor>, greater<PointColor> > color_queue;  

    cvSub(band, mask, band, NULL);         //band = band - mask
    SET_BORDER1_C1(band, 0);            

    for (int i = 0; i < band->rows; i++)
    {
        for (int j = 0; j < band->cols; j++)
        {
            if (CV_MAT_ELEM(*band, unsigned char, i, j) != 0)
            {
                color_queue.push(PointColor(i, j, 0));
            }
        }
    }

    cvSet(f, cvScalar(BAND, 0, 0, 0), band);        
    cvSet(f, cvScalar(INSIDE, 0, 0, 0), mask);  
    cvSet(t, cvScalar(0, 0, 0, 0), band);

    //Mat guidance(guidance_img);
    Mat guidance(guidance_img->rows, guidance_img->cols, guidance_img->type, guidance_img->data.fl);  
    //cout << guidance_img->rows << ";" << guidance_img->cols << ";" << guidance_img->type << endl;
    Mat marching_speed(input_img->rows, input_img->cols, CV_32F);
    Mat guidance_img1 = guidance.clone();
    Mat guassion_guidance, guassion_guidance_gray;
    vector<Mat> rgb_split(3);
    Mat kern = getGaussianKernel(3, 1);
    filter2D(guidance_img1, guassion_guidance, guidance_img1.depth(), kern);  
    cvtColor(guassion_guidance, guassion_guidance_gray, CV_RGB2GRAY);     
    guassion_guidance.convertTo(guassion_guidance, CV_32F);
    split(guassion_guidance, rgb_split);                                    

    Mat grad_x, grad_y;
    Mat abs_grad_x, abs_grad_y;
    Mat grad_xy;
    Mat grad_sum(input_img->rows, input_img->cols, CV_32F);
    for (int i = 0; i < 3; i++)
    {
        /// Gradient X
        Sobel(rgb_split[i], grad_x, CV_32F, 1, 0);
        convertScaleAbs(grad_x, abs_grad_x);

        /// Gradient Y
        Sobel(rgb_split[i], grad_y, CV_32F, 0, 1);
        convertScaleAbs(grad_y, abs_grad_y);

        /// Total Gradient (approximate)
        addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad_xy);

        grad_xy.convertTo(grad_xy, CV_32F);
        if (i == 0)
            grad_sum = grad_xy.mul(grad_xy);
        else
            grad_sum += grad_xy.mul(grad_xy);
    }

    grad_sum.convertTo(grad_sum, CV_32F);
    marching_speed = 1 / (1 + (grad_sum / (SPEED_SCALE * SPEED_SCALE)));
    //marching_speed = exp(-(grad_sum/(parameter_k*parameter_k))); //default parameter_k = 100

    icvTeleaInpaintFMM(depth, guidance, mask, output_img, range, t, marching_speed, color_queue, order);
}  //Breakpoint after running this function program

In debug mode, when I enter the disassembler to step through the breakpoints and skip the breakpoints, the program can run normally, but the execution of the program directly or the debugger will cause abnormal breakpoints. In addition, I run the program on another computer with Ubuntu system without any problems.

visual-studio
debugging
heap-corruption
opencv4
asked on Stack Overflow Dec 11, 2019 by yichen • edited Dec 11, 2019 by yichen

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0