From ce7f269f9cd797368b1908a64dea5c4de5d0999b Mon Sep 17 00:00:00 2001 From: Asi Bross Date: Sun, 10 May 2015 11:56:59 +0300 Subject: [PATCH] Missing smart pointer handling in Java director extra methods implementation. Added proper smart pointer casting to the generated *change_ownership method. Fixing issue #370. --- Source/Modules/java.cxx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index 8fbdb13e4..6911076a1 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -3521,9 +3521,20 @@ public: Printf(code_wrap->def, "SWIGEXPORT void JNICALL Java_%s%s_%s(JNIEnv *jenv, jclass jcls, jobject jself, jlong objarg, jboolean jtake_or_release) {\n", jnipackage, jni_imclass_name, changeown_jnimethod_name); - Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", norm_name, norm_name); - Printf(code_wrap->code, " %s *director = dynamic_cast<%s *>(obj);\n", dirClassName, dirClassName); - Printf(code_wrap->code, " (void)jcls;\n"); + + if (Len(smartptr)) { + Printf(code_wrap->code, " %s *obj = *((%s **)&objarg);\n", smartptr, smartptr); + Printf(code_wrap->code, " (void)jcls;\n"); + Printf(code_wrap->code, " // Keep a local instance of the smart pointer around while we are using the raw pointer\n"); + Printf(code_wrap->code, " // Avoids using smart pointer specific API.\n"); + Printf(code_wrap->code, " %s *director = dynamic_cast<%s *>(obj->operator->());\n", dirClassName, dirClassName); + } + 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, " %s *director = dynamic_cast<%s *>(obj);\n", dirClassName, dirClassName); + } + Printf(code_wrap->code, " if (director) {\n"); Printf(code_wrap->code, " director->swig_java_change_ownership(jenv, jself, jtake_or_release ? true : false);\n"); Printf(code_wrap->code, " }\n");