double free or corruption (!prev) c++ opencv

asked 2016-04-24 12:34:50 -0500

choukri el gravatar image

I'm implementing a method that decomposes the images,it takes an image as input and returns many images as output called BEMCs.Here is my main function where I try to return just the first BEMC :

int main(int argc, char **argv)
{
if (argc != 2) {
    std::cout << "Usage: ./emd <image>" << std::endl;
    return 1;
}
cv::Mat inputImg;
cv::Mat imgMode;

inputImg=imread(argv[1],CV_LOAD_IMAGE_COLOR); 

if(! inputImg.data )                             
{   cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}
namedWindow("Source Image",WINDOW_AUTOSIZE);
imshow("Source Image",inputImg);
cv::waitKey(1000);

Mat gray;
cvtColor(inputImg,gray,COLOR_BGR2GRAY);
Mat grayy;
gray.convertTo(grayy, CV_32F);
sprintf(modeTitle, "BEMC-%d", 1);
std::cout << "Decomposition " << modeTitle << std::endl;
cv::Mat imgMod(grayy) , result;

imgMod = decompose(grayy); *************main.cpp:387********                                                 
//**** decompose is the function that generate the error******
...........................
...........................
}

Here's a portion of my function decompose,first I'm trying to find maximas of the image ,then I'm trying to store them into vectors that I use to do other things:

cv::Mat decompose(cv::Mat input )
{
cv::Mat inputImg;
input.copyTo(inputImg);

std::vector<Euclidean> vectEMax, vectEMin;

cv::Mat imgMax;
...................................
vectEMax.push_back(max);vectEMax.push_back(min);

 ................................

 std::vector<Euclidean>::iterator it1, it2;

 ..............................

I'm using the iteretors to calculate distances between maximas,all these operations work fine,I insert the elements in vectEMax and calculate without any problems. At the end of the program I have to return an image as a result of the method .

cv::Mat  imgMoyenne //imgMoyenne is an image based on maximas,calculted  
                         in the program
    ....................
    cv::Mat diff_im;
    inputImg.copyTo(diff_im);
    diff_im = inputImg - imgMoyenne ;
    return diff_im;}*****************main.cpp:345**************

The program crashes after the return,it shows * glibc detected * ./gdb_core: double free or corruption (!prev): 0x08c33d78 * here is a gdb output

Program terminated with signal 6, Aborted.
  #0  0xb7738424 in __kernel_vsyscall ()

  thread apply all bt

 Thread 1 (Thread 0xb4282740 (LWP 3652)):
 #0  0xb773a424 in __kernel_vsyscall ()
 #1  0xb6f1f1df in __GI_raise (sig=6)
 at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
 #2  0xb6f22825 in __GI_abort () at abort.c:91
 #3  0xb6f5c39a in __libc_message (do_abort=2, 
 fmt=0xb70578e8 "*** glibc detected *** %s: %s: 0x%s ***\n")
 at ../sysdeps/unix/sysv/linux/libc_fatal.c:201
 #4  0xb6f66ee2 in malloc_printerr (action=<optimized out>, 
 str=<optimized out>, ptr=0x8c33d78) at malloc.c:5039
#5  0xb7549c22 in cv::fastFree(void*) ()
from /usr/local/lib/libopencv_core.so.2.4
#6  0xb763e78b in cv::Mat::deallocate() ()
from /usr/local/lib/libopencv_core.so.2.4
#7  0x0804c1fd in cv::Mat::release (this=0xbfda1fc8)
at /usr/local/include/opencv2/core/mat.hpp:367
#8  0x0804c055 in cv::Mat::~Mat (this=0xbfda1fc8, __in_chrg=<optimized out>)
at /usr/local/include/opencv2/core/mat.hpp:276
#9  0x0804b24c in decompose (input=...) at main.cpp:345
#10 0x0804b87f in main (argc=2, argv=0xbfda25a4) at main.cpp:387

I need your help please

edit retag flag offensive close merge delete

Comments

your code unfortunately does not show anything helpful, and errors like that usually hint at a buffer overrun completely elsewhere in your code.

berak gravatar imageberak ( 2016-04-24 13:34:25 -0500 )edit
choukri el gravatar imagechoukri el ( 2016-04-24 14:02:52 -0500 )edit
1

don't new the return Mat in both places (inside decompose function and where you call it), Btw there is really no need to use new anyway.

boaz001 gravatar imageboaz001 ( 2016-04-25 03:22:15 -0500 )edit

your code in the pastebin differs significantly from what you show here (all code above is useless)

and yea, do not use pointers to cv::Mat, and avoid calling new on them.

(and honestly, it's a bloody mess.)

berak gravatar imageberak ( 2016-04-25 03:35:16 -0500 )edit