Skip to content

Mixed raw pointers and Ptr<> in features2d java wrappers #11268

@berak

Description

@berak
System information (version)
  • OpenCV => 3.4.1-dev (master)
  • Operating System / Platform => Windows 64 Bit
  • Compiler => ming64
Detailed description

both FlannBasedMatcher and BFMatcher have a public constructor and a create() method, the 1st constructs a new FlannBasedMatcher , the 2nd a new Ptr<Ptr<FlannbasedMatcher>>.
since the internal wrapper code expects the latter, using an instance created from a constructor will segfault
when being deferenced like this:

  Ptr<cv::DescriptorMatcher>* me = (Ptr<cv::DescriptorMatcher>*) self; 

a similar problem occurs in the BOWKMeansTrainer class, which can only be invoked from a constructor, but the internal code uses dereferenced Ptr

java example code:

FlannbasedMatcher matcher = new FlannbasedMatcher();
matcher.match(descriptor1, descriptor2, matches); // segfaults

BOWKMeansTrainer trainer = new BOWKMeansTrainer(100);
Mat vocab = trainer.cluster(descriptors); // segfaults

generated jni code:

JNIEXPORT jlong JNICALL Java_org_opencv_features2d_FlannBasedMatcher_FlannBasedMatcher_10
  (JNIEnv* env, jclass )
{
    static const char method_name[] = "features2d::FlannBasedMatcher_10()";
    try {
        LOGD("%s", method_name);
        
        cv::FlannBasedMatcher* _retval_ = new cv::FlannBasedMatcher( makePtr<flann::KDTreeIndexParams>(), makePtr<flann::SearchParams>() );
        return (jlong) _retval_;
    } catch(const std::exception &e) {
        throwJavaException(env, &e, method_name);
    } catch (...) {
        throwJavaException(env, 0, method_name);
    }
    return 0;
}

JNIEXPORT void JNICALL Java_org_opencv_features2d_DescriptorMatcher_knnMatch_11
  (JNIEnv* env, jclass , jlong self, jlong queryDescriptors_nativeObj, jlong trainDescriptors_nativeObj, jlong matches_mat_nativeObj, jint k)
{
    static const char method_name[] = "features2d::knnMatch_11()";
    try {
        LOGD("%s", method_name);
        std::vector< std::vector<DMatch> > matches;
        Mat& matches_mat = *((Mat*)matches_mat_nativeObj);
        Ptr<cv::DescriptorMatcher>* me = (Ptr<cv::DescriptorMatcher>*) self; //TODO: check for NULL
        Mat& queryDescriptors = *((Mat*)queryDescriptors_nativeObj);
        Mat& trainDescriptors = *((Mat*)trainDescriptors_nativeObj);
        (*me)->knnMatch( queryDescriptors, trainDescriptors, matches, (int)k );
        vector_vector_DMatch_to_Mat( matches, matches_mat );
        return;
    } catch(const std::exception &e) {
        throwJavaException(env, &e, method_name);
    } catch (...) {
        throwJavaException(env, 0, method_name);
    }
    return;
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions