double free or corruption mistake

asked 2018-09-09 15:59:44 -0500

Aiyyaa gravatar image

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;
    cv::hconcat(img, lastcol, img);

  if (img.rows % 2 != 0){
    Mat 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)<uchar>(i,j)+(int)<uchar>(i,j+1))/2;
            float tmph = (-(int)<uchar>(i,j)+(int)<uchar>(i,j+1))/2;
  <float>(i,j) = tmpl;
  <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 = (<float>(i,j)<float>(i+1,j))/2;
            float tmplh = (<float>(i,j)<float>(i+1,j))/2;
            float tmphl = (<float>(i,j+img.cols/2)<float>(i+1,j+img.cols/2))/2;
            float tmphh = (<float>(i,j+img.cols/2)<float>(i+1,j+img.cols/2))/2;
  <float>(i,j) = tmpll;
  <float>(i,j+img.cols/2) = tmplh;
  <float>(i+img.rows/2,j) = tmphl;
  <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/
    #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.

edit retag flag offensive close merge delete


throw it all away, and learn how to use a dct.

writing for loops like that should be entirely forbidden to noobs.

you're using like j+1 and even j+img.cols/2 but forgot to adjust the loop range, so you're out of bounds

berak gravatar imageberak ( 2018-09-10 00:21:44 -0500 )edit