Segmentation fault with FlannBasedMatcher in python (Bug #4385)
Description
Since updating to opencv 3.0 we are getting a segmentation fault with the following code in most of our servers (Oddly it's working in one of them).
This is a sample code to reproduce the crash:
import cv2
FLANN_INDEX_LSH = 6
detector = cv2.ORB_create(4000, scaleFactor=1.05, nlevels=20)
img = cv2.imread("danone.jpg", cv2.IMREAD_GRAYSCALE)
kp, desc = detector.detectAndCompute(img, None)
flann_params = dict(algorithm=FLANN_INDEX_LSH,
table_number=3, # 6
key_size=20, # 12
multi_probe_level=1) # 1
search_params = {}
matcher = cv2.FlannBasedMatcher(flann_params, search_params) # bug : need to pass empty dict (#1329)
all_desc = [desc]
print "A"
matcher.clear()
print "B"
matcher.add(all_desc)
print "C"
matcher.train()
print "D"
matcher.getTrainDescriptors()
print "E"
Output:
A
B
C
D
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff67ce229 in _object* pyopencv_from<cv::Mat>(cv::Mat const&) ()
from /home/user/Downloads/opencv-3.0.0/build/lib/cv2.so
History
Updated by Maksim Shabunin almost 10 years ago
I couldn't reproduce this issue, tried your test code several times (I used lena.jpg, because you haven't attached your image).
Please, provide more information about your confguration and the test image.
- Status changed from New to Incomplete
Updated by warmax gonzalez almost 10 years ago
Hello Maksim,
The problem happens with any image. We have a feeling that it must be something related to the compilation process of Opencv (Some lib maybe?).
Server is using Ubuntu 14.04.2 x64 but I can also reproduce it with other Ubuntu based distributions.
When I change the binaries to use version 2.4.10 everything works fine...
I upload version_string file so you can check how it was compiled:
http://pastebin.com/YCJdWBKE
Any ideas on how to proceed?
Greetings
Updated by Maksim Shabunin over 9 years ago
I've found invalid memory access when running your example. Memory is released twice: in FlannBasedMatcher::add and in python wrapper. But I'm not sure where the actual problem is.
==20149== Invalid read of size 4 ==20149== at 0x69A4021: cv::Mat::release() (mat.inl.hpp:655) ==20149== by 0x69A3CE3: cv::Mat::~Mat() (mat.inl.hpp:543) ==20149== by 0x6A7D41C: void std::_Destroy<cv::Mat>(cv::Mat*) (stl_construct.h:93) ==20149== by 0x6A7B01F: void std::_Destroy_aux<false>::__destroy<cv::Mat*>(cv::Mat*, cv::Mat*) (stl_construct.h:103) ==20149== by 0x6A77D43: void std::_Destroy<cv::Mat*>(cv::Mat*, cv::Mat*) (stl_construct.h:126) ==20149== by 0x6A72166: void std::_Destroy<cv::Mat*, cv::Mat>(cv::Mat*, cv::Mat*, std::allocator<cv::Mat>&) (stl_construct.h:151) ==20149== by 0x6A67E47: std::vector<cv::Mat, std::allocator<cv::Mat> >::~vector() (stl_vector.h:415) ==20149== by 0x69F6119: pyopencv_cv_DescriptorMatcher_add(_object*, _object*, _object*) (pyopencv_generated_types.h:15429) ==20149== by 0x52C6D4: PyEval_EvalFrameEx (in /usr/bin/python2.7) ==20149== by 0x55C593: PyEval_EvalCodeEx (in /usr/bin/python2.7) ==20149== by 0x5B7391: PyEval_EvalCode (in /usr/bin/python2.7) ==20149== by 0x469662: ??? (in /usr/bin/python2.7) ==20149== Address 0x25292db4 is 20 bytes inside a block of size 80 free'd ==20149== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20149== by 0x69A5564: NumpyAllocator::deallocate(cv::UMatData*) const (cv2.cpp:198) ==20149== by 0x749909D: cv::UMat::deallocate() (umatrix.cpp:303) ==20149== by 0x72E17C4: cv::UMat::release() (mat.inl.hpp:3297) ==20149== by 0x7499031: cv::UMat::~UMat() (umatrix.cpp:296) ==20149== by 0x732C4EA: void std::_Destroy<cv::UMat>(cv::UMat*) (stl_construct.h:93) ==20149== by 0x73271D9: void std::_Destroy_aux<false>::__destroy<cv::UMat*>(cv::UMat*, cv::UMat*) (stl_construct.h:103) ==20149== by 0x7321B62: void std::_Destroy<cv::UMat*>(cv::UMat*, cv::UMat*) (stl_construct.h:126) ==20149== by 0x7319728: void std::_Destroy<cv::UMat*, cv::UMat>(cv::UMat*, cv::UMat*, std::allocator<cv::UMat>&) (stl_construct.h:151) ==20149== by 0x7417207: std::vector<cv::UMat, std::allocator<cv::UMat> >::~vector() (stl_vector.h:415) ==20149== by 0x93DED91: cv::FlannBasedMatcher::add(cv::_InputArray const&) (matchers.cpp:1031) ==20149== by 0x69F60C6: pyopencv_cv_DescriptorMatcher_add(_object*, _object*, _object*) (pyopencv_generated_types.h:15425) ==20149==
Updated by Maksim Shabunin over 9 years ago
Also, replacing UMat to Mat in FlannBasedMatcher::add:
std::vector<UMat> descriptors; _descriptors.getUMatVector(descriptors);
fixes the problem.
Updated by warmax gonzalez over 9 years ago
Maksim Shabunin wrote:
Also, replacing UMat to Mat in FlannBasedMatcher::add:
[...]
fixes the problem.
Thank you, that fixed it!
Updated by Alexander Alekhin over 9 years ago
- Status changed from Incomplete to Done
- Assignee changed from Marius Muja to Maksim Shabunin
- Pull request set to https://github.com/Itseez/opencv/pull/4111