From 540ede0dd36896f0c26ee0f1cbd7ba705db8eacc Mon Sep 17 00:00:00 2001 From: "Brant K. Kyser" Date: Thu, 3 Jan 2013 14:54:06 -0600 Subject: [PATCH] Fix for SourceForge Bug #1283. * Change the name of the memory own variable for base java director classes to match that expected by the director code * Add conditional to appropriately dynamically cast director classes wrapped in smart pointers. --- Lib/java/boost_shared_ptr.i | 8 ++++---- Source/Modules/java.cxx | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Lib/java/boost_shared_ptr.i b/Lib/java/boost_shared_ptr.i index 31d7efdf9..e75236993 100644 --- a/Lib/java/boost_shared_ptr.i +++ b/Lib/java/boost_shared_ptr.i @@ -146,10 +146,10 @@ // Base proxy classes %typemap(javabody) TYPE %{ private long swigCPtr; - private boolean swigCMemOwnBase; + private boolean swigCMemOwn; PTRCTOR_VISIBILITY $javaclassname(long cPtr, boolean cMemoryOwn) { - swigCMemOwnBase = cMemoryOwn; + swigCMemOwn = cMemoryOwn; swigCPtr = cPtr; } @@ -176,8 +176,8 @@ %typemap(javadestruct, methodname="delete", methodmodifiers="public synchronized") TYPE { if (swigCPtr != 0) { - if (swigCMemOwnBase) { - swigCMemOwnBase = false; + if (swigCMemOwn) { + swigCMemOwn = false; $jnicall; } swigCPtr = 0; diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index 971e60659..dbe569643 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -3381,6 +3381,7 @@ public: String *swig_director_connect = Swig_name_member(getNSpace(), proxy_class_name, "director_connect"); String *swig_director_connect_jni = makeValidJniName(swig_director_connect); String *sym_name = Getattr(n, "sym:name"); + String *smartptr_feature = Getattr(n, "feature:smartptr"); Wrapper *code_wrap; Printf(imclass_class_code, " public final static native void %s(%s obj, long cptr, boolean mem_own, boolean weak_global);\n", @@ -3390,9 +3391,24 @@ public: Printf(code_wrap->def, "SWIGEXPORT void JNICALL Java_%s%s_%s(JNIEnv *jenv, jclass jcls, jobject jself, jlong objarg, jboolean jswig_mem_own, " "jboolean jweak_global) {\n", jnipackage, jni_imclass_name, swig_director_connect_jni); - Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", norm_name, norm_name); - Printf(code_wrap->code, " (void)jcls;\n"); - Printf(code_wrap->code, " SwigDirector_%s *director = dynamic_cast(obj);\n", sym_name, sym_name); + + if (Len(smartptr_feature)) { + Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", smartptr_feature, smartptr_feature); + Printf(code_wrap->code, " (void)jcls;\n"); + Printf(code_wrap->code, " // NOTE: Pulling the raw pointer out of the smart pointer as the following code does\n"); + Printf(code_wrap->code, " // is generally a bad idea. However, in this case we keep a local instance of the\n"); + Printf(code_wrap->code, " // smart pointer around while we are using the raw pointer, which should keep the\n"); + Printf(code_wrap->code, " // raw pointer alive. This is done instead of using the smart pointer's dynamic cast\n"); + Printf(code_wrap->code, " // feature since different smart pointer implementations have differently named dynamic\n"); + Printf(code_wrap->code, " // cast mechanisms.\n"); + Printf(code_wrap->code, " SwigDirector_%s *director = dynamic_cast< SwigDirector_%s *>(obj->operator->());\n", sym_name, sym_name); + } + else { + Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", norm_name, norm_name); + Printf(code_wrap->code, " (void)jcls;\n"); + Printf(code_wrap->code, " SwigDirector_%s *director = dynamic_cast(obj);\n", sym_name, sym_name); + } + Printf(code_wrap->code, " if (director) {\n"); Printf(code_wrap->code, " director->swig_connect_director(jenv, jself, jenv->GetObjectClass(jself), " "(jswig_mem_own == JNI_TRUE), (jweak_global == JNI_TRUE));\n");