OpenCV free():指针无效:

时间:2013-06-30 10:35:51

标签: c++ pointers opencv memory-leaks runtime-error

运行从文件读取一些数据的应用程序时,我收到下一个错误,提取图像的特征并使用一组预先训练的分类器预测结果:

> *** glibc detected *** ./SVMread: free(): invalid pointer: 0xbfeb2670 ***
> ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb5fe8ee2]
> /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb61e451f]
> ./SVMread(_ZN2cv17FlannBasedMatcherD0Ev+0x1c)[0x804b214]
> ./SVMread[0x804ca96] ./SVMread[0x804c0e1]
> ./SVMread(_ZN2cv3PtrINS_17DescriptorMatcherEED1Ev+0x11)[0x804b6e9]
> ./SVMread[0x804a8cc]
> /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb5f8c4d3]
> ./SVMread[0x8049ef1]
> ======= Memory map: ======== 08048000-08050000 r-xp 00000000 00:14 101213     /home/srt/OpenCV-2.4.2/samples/cpp/SVMread
> 08050000-08051000 r--p 00007000 00:14 101213    
> /home/srt/OpenCV-2.4.2/samples/cpp/SVMread 08051000-08052000 rw-p
> 00008000 00:14 101213     /home/srt/OpenCV-2.4.2/samples/cpp/SVMread
> ...

我认为冲突部分是代码的下一部分之一,但我真的无法在Web上找到解决方案,也无法在OpenCV文档中找到解决方案。

     SurfDescriptorExtractor extractor1;
     Ptr<DescriptorExtractor> extractor = &extractor1;
     FlannBasedMatcher matcher1;
     Ptr<DescriptorMatcher> matcher = &matcher1;

     BOWImgDescriptorExtractor bowide(extractor,matcher);

或在:

CvFileStorage* storage = cvOpenFileStorage( "svm1.yml", 0, CV_STORAGE_READ );

 const char * c;
 string clase;
 CvFileNode*  cFN;

 for (unsigned int i = 0; i<Vclases.size(); i++) {              
  clase = Vclases[i];
  c = (clase).c_str();                      
  cFN = cvGetFileNodeByName( storage , 0, c );          
  cout << "reading  " << clase  <<  endl;
  classes_classifiers[clase].read(storage, cFN );           

 }

编辑:valgrind输出确认存在以下问题:

CvFileStorage* storage = cvOpenFileStorage( "svm1.yml", 0, CV_STORAGE_READ );

Valgrind的:

> Conditional jump or move depends on uninitialised value(s)
> ==23398==    at 0x56585C0: ____strtod_l_internal (strtod_l.c:1659)
> ==23398==    by 0x5653FAE: strtod (strtod.c:70)
> ==23398==    by 0x41425B1: icv_strtod(CvFileStorage*, char*, char**) (in /usr/local/lib/libopencv_core.so.2.4.2)
> ==23398==    by 0x415875F: icvYMLParseValue(CvFileStorage*, char*, CvFileNode*, int, int) (in /usr/local/lib/libopencv_core.so.2.4.2)
> ==23398==    by 0x804A377: main (SVMread.cpp:64)
> ==23398==  Uninitialised value was created by a stack allocation
> ==23398==    at 0x56575E4: ____strtod_l_internal (strtod_l.c:424)

我是指针和内存分配的新手,所以任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:问题是cv:Ptr与dinamically分配的对象一起工作,这就是为什么在尝试释放由引用初始化的指针时发生错误的原因。因此,正确的指针声明是:

 Ptr<DescriptorMatcher > matcher = new FlannBasedMatcher;
 Ptr<DescriptorExtractor > extractor(new SurfDescriptorExtractor());

分段错误消失了。

(注意:我找到了解决方案,这要归功于这里的答案:OpenCV/C++ - How to free pointers