繁体   English   中英

OpenCV / C ++-如何释放指针

[英]OpenCV/C++ - How to free pointers

为了能够调用特定函数,我不得不做一个涉及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);


题:

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

提前致谢。

cv::Ptr用于动态分配的对象。 extractor1matcher1不会动态分配。 超出范围时,它们将自动释放。

代替旧版API

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

使用OpenCV 2 API

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

那么您不必担心指针。

请参阅OpenCV文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM