无法传递 boost::shared_ptr<>进入 NewObject()

发布于 2025-01-05 04:12:29 字数 1838 浏览 0 评论 0 原文

也许我在这里太过雄心勃勃,但我试图通过像这样的异常将shared_ptr传回Java。

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

有什么见解吗?

谢谢!

-芯片

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());

}

Perhaps I'm being too ambitious here, but I'm trying to pass a shared_ptr back to Java through an Exception like so.

I am able to catch the Exception in java, but when I try to access any methods in the ManagementProcessor object itself I get a SIGSEGV. If I use new ManagementProcessorPtr() to send in an empty one I get the correct behavior (I throw a different exception).

Any insights?

Thanks!

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

Client code:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙幽之幻 2025-01-12 04:12:29

boost::shared_ptr 是一个 C++ 。是什么让您认为它与 ManagementProcessorPtr* 相同?

a boost::shared_ptr is a c++ class. what makes you think that it is the same as a ManagementProcessorPtr*?

天赋异禀 2025-01-12 04:12:29

当然,正确的答案是,首先我必须使用采用 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());

And of course the right answer is that first I have to create the Java ManagementProcessor object with the ctor that takes a CPtr:

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文