2012-02-13 82 views
1

也許我在這裏過於雄心勃勃,但我試圖通過像這樣的Exception將shared_ptr傳回Java。無法將boost :: shared_ptr <>傳入NewObject()

我能夠在java中捕獲異常,但是當我嘗試訪問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()); 

}

回答

1

當然,正確的答案是,首先我要創建與需要CPTR的構造函數在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()); 
1

一個boost::shared_ptr是一個C++類。是什麼讓你認爲它和ManagementProcessorPtr*一樣?

+1

我知道他們是不一樣。我應該傳入構造函數?我看到SWIG生成的代碼使用*(VolumePtr **)&jresult = new VolumePtr((const VolumePtr&)result)形式的東西; 到處都是。 – 2012-02-13 22:21:19

相關問題