Str1101
Str1101

Reputation: 899

OpenCV/C++ - How to free pointers

I had to do a trick involving cv::Ptr (pointers) in order to be able to call a specific function, but it seems that the program has some problem when tries to free the memory after the program execution because a segmentation fault happens.


1.Code:

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

2.Errors:

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.Note: The first error belongs to the next declaration (again a pointer), which the error output classifies as an "uninitialised value created by a stack allocation". Is that true? If so, how can I fix it?

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


Question:

Is there some function or trick that I need to use in order to free the memory of the pointers or of any other element before the program tries to do it and fails?

Thanks in advance.

Upvotes: 1

Views: 2840

Answers (2)

Vaughn Cato
Vaughn Cato

Reputation: 64308

cv::Ptr is for dynamically allocated objects. extractor1, and matcher1 aren't dynamically allocated. They will be freed automatically when they go out of scope.

Upvotes: 5

Bull
Bull

Reputation: 11941

Instead of the legacy API

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

use the OpenCV 2 API

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

then you don't need to worry about pointers.

See the OpenCV docs

Upvotes: 1

Related Questions