0

为了能够调用特定的函数,我不得不做一个涉及 cv::Ptr (指针)的技巧,但是由于发生分段错误,在程序执行后尝试释放内存时,程序似乎有一些问题。


1.代码:

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

2.错误:

Conditional jump or move depends on uninitialised value(s)
==23559==    at 0x56585C0: ____strtod_l_internal (strtod_l.c:1659)
==23559==    by 0x5653FAE: strtod (strtod.c:70)
==23559==    by 0x41425B1: icv_strtod(CvFileStorage*, char*, char**) (in /usr/local/lib/libopencv_core.so.2.4.2)
==23559==    by 0x415875F: icvYMLParseValue(CvFileStorage*, char*, CvFileNode*, int, int) (in /usr/local/lib/libopencv_core.so.2.4.2)
==23559==    by 0x4158BA4: icvYMLParseValue(CvFileStorage*, char*, CvFileNode*, int, int) (in /usr/local/lib/libopencv_core.so.2.4.2)
==23559==    by 0x41593F7: icvYMLParseValue(CvFileStorage*, char*, CvFileNode*, int, int) (in /usr/local/lib/libopencv_core.so.2.4.2)
==23559==    by 0x41593F7: icvYMLParseValue(CvFileStorage*, char*, CvFileNode*, int, int) (in /usr/local/lib/libopencv_core.so.2.4.2)
==23559==    by 0x4159DE0: cvOpenFileStorage (in /usr/local/lib/libopencv_core.so.2.4.2)
==23559==    by 0x804A377: main (SVMread.cpp:66)
==23559==  Uninitialised value was created by a stack allocation
==23559==    at 0x56575E4: ____strtod_l_internal (strtod_l.c:424)
==23559== 
==23559== Invalid read of size 4
==23559==    at 0x43503B8: cvflann::KDTreeIndex<cvflann::L2<float> >::findNeighbors(cvflann::ResultSet<float>&, float const*, cvflann::SearchParams const&) (in /usr/local/lib/libopencv_flann.so.2.4.2)
==23559==    by 0x43339E6: cvflann::NNIndex<cvflann::L2<float> >::knnSearch(cvflann::Matrix<float> const&, cvflann::Matrix<int>&, cvflann::Matrix<float>&, int, cvflann::SearchParams const&) (in /usr/local/lib/libopencv_flann.so.2.4.2)
==23559==    by 0x431D5C9: cvflann::Index<cvflann::L2<float> >::knnSearch(cvflann::Matrix<float> const&, cvflann::Matrix<int>&, cvflann::Matrix<float>&, int, cvflann::SearchParams const&) (in /usr/local/lib/libopencv_flann.so.2.4.2)
==23559==    by 0x1: ???
==23559==  Address 0xbeb8774c is just below the stack ptr.  To suppress, use: --workaround-gcc296-bugs=yes
==23559== 
==23559== Invalid free() / delete / delete[] / realloc()
==23559==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)
==23559==    by 0x5502245: cv::SURF::~SURF() (in /usr/local/lib/libopencv_nonfree.so.2.4.2)
==23559==    by 0x56374D2: (below main) (libc-start.c:226)
==23559==  Address 0xbeb88060 is on thread 1's stack
==23559== 
==23559== Invalid free() / delete / delete[] / realloc()
==23559==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)
==23559==    by 0x804B56F: cv::FlannBasedMatcher::~FlannBasedMatcher() (features2d.hpp:1120)
==23559==    by 0x56374D2: (below main) (libc-start.c:226)
==23559==  Address 0xbeb87e2c is on thread 1's stack
==23559== 
==23559== Invalid free() / delete / delete[] / realloc()
==23559==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)
==23559==    by 0x42AC883: cv::DescriptorMatcher::DescriptorCollection::~DescriptorCollection() (in /usr/local/lib/libopencv_features2d.so.2.4.2)
==23559==    by 0x56374D2: (below main) (libc-start.c:226)
==23559==  Address 0x8eb0bd0 is 0 bytes inside a block of size 4 free'd
==23559==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)
==23559==    by 0x42AC883: cv::DescriptorMatcher::DescriptorCollection::~DescriptorCollection() (in /usr/local/lib/libopencv_features2d.so.2.4.2)
==23559==    by 0x56374D2: (below main) (libc-start.c:226)
==23559== 

3.注意:第一个错误属于下一个声明(同样是一个指针),错误输出将其归类为“由堆栈分配创建的未初始化值”。真的吗?如果是这样,我该如何解决?

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


问题:

在程序尝试执行它并失败之前,我需要使用一些功能或技巧来释放指针或任何其他元素的内存吗?

提前致谢。

4

2 回答 2

5

cv::Ptr用于动态分配的对象。 extractor1,并且matcher1不是动态分配的。当它们超出范围时,它们将被自动释放。

于 2013-07-01T00:12:22.780 回答
1

代替旧版 API

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

使用 OpenCV 2 API

cv::FileStorage storage("svm1.yml", cv::FileStorage::READ);  

那么你不需要担心指针。

请参阅OpenCV 文档

于 2013-07-01T04:28:29.273 回答