Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

double free or corruption mistake

I have a function called coefficients2(), where I decompose image in low and high frequency components:

Mat coefficients2(Mat img){
  if (img.cols % 2 != 0){
    Mat lastcol;
    img.col(img.cols-1).copyTo(lastcol);
    cv::hconcat(img, lastcol, img);
}

  if (img.rows % 2 != 0){
    Mat lastrow;     
    img.row(img.rows-1).copyTo(lastrow);
    img.push_back(lastrow); 
  }

Mat lowhigh;
lowhigh.create(img.rows, img.cols, CV_32FC1);
 for(int i = 0; i < img.rows; i++){    
        for(int j = 0; j < img.cols; j+=2){
        float tmpl = ((int)img.at<uchar>(i,j)+(int)img.at<uchar>(i,j+1))/2;
            float tmph = (-(int)img.at<uchar>(i,j)+(int)img.at<uchar>(i,j+1))/2;
            lowhigh.at<float>(i,j) = tmpl;
            lowhigh.at<float>(i,j+img.cols/2) = tmph;
    }
}

Mat lowlowhighhigh; 
 lowlowhighhigh.create(img.rows, img.cols, CV_32FC1);
 for(int i=0; i<img.rows;i+=2){         
        for(int j=0; j<img.cols/2;j++){
            float tmpll = (lowhigh.at<float>(i,j)+lowhigh.at<float>(i+1,j))/2;
            float tmplh = (-lowhigh.at<float>(i,j)+lowhigh.at<float>(i+1,j))/2;
            float tmphl = (lowhigh.at<float>(i,j+img.cols/2)+lowhigh.at<float>(i+1,j+img.cols/2))/2;
            float tmphh = (-lowhigh.at<float>(i,j+img.cols/2)+lowhigh.at<float>(i+1,j+img.cols/2))/2;
            lowlowhighhigh.at<float>(i,j) = tmpll;
            lowlowhighhigh.at<float>(i,j+img.cols/2) = tmplh;
            lowlowhighhigh.at<float>(i+img.rows/2,j) = tmphl;
            lowlowhighhigh.at<float>(i+img.rows/2,j+img.cols/2) = tmphh;
        }       
    }  
return lowlowhighhigh;}

But this functon doesn't work, and I don't know why. Here's that I get from gdb when run this function on it

    #0  0x00007ffff6a3be97 in __GI_raise (sig=sig@entry=6)
        at ../sysdeps/unix/sysv/linux/raise.c:51
    #1  0x00007ffff6a3d801 in __GI_abort () at abort.c:79
    #2  0x00007ffff6a86897 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff6bb3b9a "%s\n") at ../sysdeps/posix/libc_fatal.c:181
    #3  0x00007ffff6a8d90a in malloc_printerr (str=str@entry=0x7ffff6bb5870 "double free or corruption (out)") at malloc.c:5350
    #4  0x00007ffff6a94e75 in _int_free (have_lock=0, p=0x7ffff7f8a030, av=0x7ffff6de8c40 <main_arena>) at malloc.c:4278
    #5  0x00007ffff6a94e75 in __GI___libc_free (mem=0x7ffff7f8a040)
        at malloc.c:3124
    #6  0x00007ffff74d7909 in cv::Mat::deallocate() ()
        at /usr/local/lib/libopencv_core.so.4.0
    #7  0x0000555555558565 in cv::Mat::release() ()

> #8  0x000055555555834c in cv::Mat::~Mat() ()
> #9  0x0000555555555cc6 in coefficients2(cv::Mat) ()
> #10 0x0000555555557d9f in main ()

But yet I don't understand where's problem exactly is. Please help me to fix my function.