c++ - 双重自由或腐败(!prev)c++ opencv

标签 c++ opencv free corruption

我正在实现一种分解图像的方法,它将图像作为输入并返回许 multimap 像作为输出,称为 BEMC。这是我的主要功能,我尝试只返回第一个 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******
...........................
...........................
}

这是我的函数分解的一部分,首先我试图找到图像的最大值,然后我试图将它们存储到我用来做其他事情的 vector 中:

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;

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

我正在使用迭代器计算最大值之间的距离,所有这些操作都工作正常,我将元素插入 vectEMax 并进行计算,没有任何问题。在程序结束时,我必须返回图像作为方法的结果。

    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**************

返回后程序崩溃,显示

* 检测到 glibc * ./gdb_core: double free or corruption (!prev): 0x08c33d78 ***

这是一个 gdb 输出

  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

我需要你的帮助

最佳答案

我知道已经有一段时间了,但我刚刚遇到了完全相同的错误。我通过逐 block 评论来找出我的代码行触发了它。

它与 cv::Mat::copyTo() 方法一起使用。如果您改用 cv::Mat::clone(),它将触发相同的错误。我摆脱了它,因为我真的不需要深拷贝。

我不知道为什么会发生这种情况。如果您检查错误消息,它会说一些关于释放内存的事情,但 OpenCV 应该会处理这个问题。

关于c++ - 双重自由或腐败(!prev)c++ opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824022/

相关文章:

c++ - 从 objective-c 转换时如何在 c 中编写变量的属性

c++ - 如何用我自己的实现替换 MS VC++ 项目中的 WinAPI 函数调用(名称和参数集相同)?

c++ - OpenCV - Brox 光流 - opencv_core244d!cv::GlBuffer::unbind 异常

python - OpenCV 找不到网络摄像头,但 Cheese 可以

c++ - 什么会导致 "corrupted double-linked list"错误?

c++ - 我对如何在 C++ 中使用 getline 和 strtok 解析多个定界符感到很困惑

c++ - vector 。检查它是否包含 "Key"。 C++

java - 为什么以及如何应用阈值来获得更好的匹配特征

c - 释放后将变量设置为 NULL

c - fclose 和 free 的顺序