2013-06-30 134 views
0

運行從文件讀取一些數據的應用程序時,出現下一個錯誤,提取圖像的特徵並使用一組預編分類器預測結果:OpenCV免費():無效指針:

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

我在考慮衝突的部分是在代碼的下一部分之一,但我真的無法找到一個解決方案在網絡上或在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) 

新手指針和內存分配,所以任何h艾爾普將不勝感激。提前致謝。

回答

0

我找到了解決方案:問題是cv:Ptr與dinamically分配的對象一起工作,這就是爲什麼在嘗試釋放由引用初始化的指針時發生錯誤。所以,正確的指針聲明是:

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

而分段錯誤消失了。

(注:我找到了解決方案,這要歸功於這裏的答案:OpenCV/C++ - How to free pointers