Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ok, I've made some progress resolving this.

Assume we are to wrap a C++ method like void foo(const Ptr<t>& arg). Whenever gen_java.py meets the function parameter of type const Ptr<t>&, it generates the following JNI code:

java:

public void foo(T arg) {
      foo_0(arg.nativeObj);
      return;
}
private static native void foo_0(long arg_nativeObj);

JNI C wrapper

JNIEXPORT void JNICALL Java_org_opencv_<module>_<class>_foo_10 (JNIEnv* env, jclass jclass, jlong arg_nativeObj) {
      static const char method_name[] = "<module>::foo_10()";
      try{
             LOGD("%s", method_name);
             <module>::foo(Ptr<T>((T*) arg_nativeObj)); // Crash happens here
      } catch(const std::exception &e) {
             throwJavaException(env, &e, method_name);
      } catch (...) {
             throwJavaException(env, 0, method_name);
      }
      return;
}

The line noteed above is leading to Java crash, However, if to replace it with the following, the crash is cured:

<module>::foo(*((Ptr<T> *) arg_nativeObj)); // Works fine

So I guess my question can be transformed to this: what is the scenario when we need to keep the original way of wrapping of const Ptr<t> & arg? Which modules/classes/methods use it?

Ok, I've made some progress resolving this.

Assume we are to wrap a C++ method like void foo(const Ptr<t>& arg). Whenever gen_java.py meets the function parameter of type const Ptr<t>&, it generates the following JNI code:

java:

public void foo(T arg) {
      foo_0(arg.nativeObj);
      return;
}
private static native void foo_0(long arg_nativeObj);

JNI C wrapper

JNIEXPORT void JNICALL Java_org_opencv_<module>_<class>_foo_10 (JNIEnv* env, jclass jclass, jlong arg_nativeObj) {
      static const char method_name[] = "<module>::foo_10()";
      try{
             LOGD("%s", method_name);
             <module>::foo(Ptr<T>((T*) arg_nativeObj)); // Crash NOTE: crash happens here
      } catch(const std::exception &e) {
             throwJavaException(env, &e, method_name);
      } catch (...) {
             throwJavaException(env, 0, method_name);
      }
      return;
}

The line noteed noted above is leading to Java crash, However, if to replace it with the following, the crash is cured:

<module>::foo(*((Ptr<T> *) arg_nativeObj)); // Works fine

So I guess my question can be transformed to this: what is the scenario when we need to keep the original way of wrapping of const Ptr<t> & arg? Which modules/classes/methods use it?