麻烦传递boost :: shared_ptr<>到NewObject()

时间:2012-02-13 21:36:20

标签: java c++ java-native-interface swig shared-ptr

也许我在这里过于雄心勃勃,但我正试图通过异常这样的方式将shared_ptr传回给Java。

我能够在java中捕获Exception,但是当我尝试访问ManagementProcessor对象本身的任何方法时,我得到了一个SIGSEGV。如果我使用新的ManagementProcessorPtr()发送一个空的,我得到正确的行为(我抛出一个不同的异常)。

任何见解?

谢谢!

-chip

typedef boost::shared_ptr<ManagementProcessor> ManagementProcessorPtr;

%include "boost_shared_ptr.i"
%shared_ptr(ManagementProcessor);
%typemap(javabase) Exception "java.lang.RuntimeException";
%typemap(javabase) AuthenticationExceptionManagementProcessor "NS/Exception";

%exception {
try {
  $action
}
catch (AuthenticationException<ManagementProcessor> & e ) {
  jclass eclass = jenv->FindClass("NS/AuthenticationExceptionManagementProcessor");
  if ( eclass ) {
    jobject excep = 0;
    jmethodID jid;

    jstring message =  jenv->NewStringUTF(e.getMessage().c_str());
    jstring file =  jenv->NewStringUTF(e.getFileName().c_str());

    ManagementProcessorPtr* realm = new ManagementProcessorPtr(e.getRealm());
    jlong jrealm;
    *(ManagementProcessorPtr **)&jrealm = realm;

    jid = jenv->GetMethodID(eclass, "<init>",
        "("
        "LNS/ManagementProcessor;"
        "J"
        "Ljava/lang/String;"
        "Ljava/lang/String;"
        "J)V");
    if (jid) {
        excep = jenv->NewObject(eclass, jid,
            jrealm,
            e.getApiErrNum(),
            message,
            file,
            e.getLineNum());
        if (excep)  {
            jenv->Throw((__jthrowable*) excep);
        }
    }
  }

客户代码:

} catch (AuthenticationExceptionManagementProcessor e) {
        java.lang.System.err.println(e);
        ManagementProcessor mp = e.getRealm();
        java.lang.System.err.println("got mp");
        java.lang.System.out.println(mp.getUUID());

}

2 个答案:

答案 0 :(得分:1)

boost::shared_ptr是c ++ 。是什么让你认为它与ManagementProcessorPtr*相同?

答案 1 :(得分:1)

当然,正确答案是首先我必须使用带有CPtr的ctor创建Java ManagementProcessor对象:

jclass mpclass = jenv->FindClass("NS/ManagementProcessor");
jobject jmp = 0;
jmethodID mpid;
ManagementProcessorPtr *realm = new ManagementProcessorPtr(e.getRealm());
jlong jrealm;
*(ManagementProcessorPtr **)&jrealm = realm;
mpid = jenv->GetMethodID(mpclass, "<init>", "(JZ)V");
jmp = jenv->NewObject(mpclass, mpid, jrealm, true);

...

        excep = jenv->NewObject(eclass, jid,
            jmp,
            e.getApiErrNum(),
            message,
            file,
            e.getLineNum());