Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
berak opened this issue Apr 8, 2018 · 0 comments · Fixed by #17428
Closed

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

berak opened this issue Apr 8, 2018 · 0 comments · Fixed by #17428
Assignees
Labels
affected: 3.4 bug category: java bindings Hackathon https://opencv.org/opencv-hackathon-starts-next-week/ priority: high
Milestone

Comments

@berak
Copy link
Contributor

berak commented Apr 8, 2018

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;
}

@asmorkalov asmorkalov added the Hackathon https://opencv.org/opencv-hackathon-starts-next-week/ label Jan 14, 2020
@asmorkalov asmorkalov self-assigned this May 27, 2020
@asmorkalov asmorkalov changed the title minor bugs in features2d java wrappers Mixed raw pointers and Ptr<> in features2d java wrappers May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected: 3.4 bug category: java bindings Hackathon https://opencv.org/opencv-hackathon-starts-next-week/ priority: high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants