summaryrefslogtreecommitdiff
path: root/platform/android/java_glue.cpp
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2019-02-27 14:08:42 +0100
committerGitHub <noreply@github.com>2019-02-27 14:08:42 +0100
commit36de6c1e3c08a30a7579148ee5d2f8ed8021de42 (patch)
tree2999e30b428a6f7adb063d9cb5bc7f5dc312b312 /platform/android/java_glue.cpp
parentf1e3bb0a159636c6c42701da9ed843d859fcbf2a (diff)
parent1073f0b4238e8c6008a2b7e7e63e71b1335f29b2 (diff)
Merge pull request #26328 from ivanarh/master
Android: Fixed problems with memory leaks due to absent ReleaseStringUTFChars calls.
Diffstat (limited to 'platform/android/java_glue.cpp')
-rw-r--r--platform/android/java_glue.cpp54
1 files changed, 32 insertions, 22 deletions
diff --git a/platform/android/java_glue.cpp b/platform/android/java_glue.cpp
index 885fb185a7..53c909da88 100644
--- a/platform/android/java_glue.cpp
+++ b/platform/android/java_glue.cpp
@@ -41,6 +41,7 @@
#include "main/input_default.h"
#include "main/main.h"
#include "os_android.h"
+#include "string_android.h"
#include "thread_jandroid.h"
#include <unistd.h>
@@ -223,7 +224,7 @@ String _get_class_name(JNIEnv *env, jclass cls, bool *array) {
jboolean isarr = env->CallBooleanMethod(cls, isArray);
(*array) = isarr ? true : false;
}
- String name = env->GetStringUTFChars(clsName, NULL);
+ String name = jstring_to_string(clsName, env);
env->DeleteLocalRef(clsName);
return name;
@@ -241,7 +242,7 @@ Variant _jobject_to_variant(JNIEnv *env, jobject obj) {
if (name == "java.lang.String") {
- return String::utf8(env->GetStringUTFChars((jstring)obj, NULL));
+ return jstring_to_string((jstring)obj, env);
};
if (name == "[Ljava.lang.String;") {
@@ -252,7 +253,7 @@ Variant _jobject_to_variant(JNIEnv *env, jobject obj) {
for (int i = 0; i < stringCount; i++) {
jstring string = (jstring)env->GetObjectArrayElement(arr, i);
- sarr.push_back(String::utf8(env->GetStringUTFChars(string, NULL)));
+ sarr.push_back(jstring_to_string(string, env));
env->DeleteLocalRef(string);
}
@@ -487,7 +488,7 @@ public:
case Variant::STRING: {
jobject o = env->CallObjectMethodA(instance, E->get().method, v);
- ret = String::utf8(env->GetStringUTFChars((jstring)o, NULL));
+ ret = jstring_to_string((jstring)o, env);
env->DeleteLocalRef(o);
} break;
case Variant::POOL_STRING_ARRAY: {
@@ -634,20 +635,20 @@ static String _get_user_data_dir() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(godot_io, _getDataDir);
- return String(env->GetStringUTFChars(s, NULL));
+ return jstring_to_string(s, env);
}
static String _get_locale() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(godot_io, _getLocale);
- return String(env->GetStringUTFChars(s, NULL));
+ return jstring_to_string(s, env);
}
static String _get_clipboard() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(_godot_instance, _getClipboard);
- return String(env->GetStringUTFChars(s, NULL));
+ return jstring_to_string(s, env);
}
static void _set_clipboard(const String &p_text) {
@@ -661,7 +662,7 @@ static String _get_model() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(godot_io, _getModel);
- return String(env->GetStringUTFChars(s, NULL));
+ return jstring_to_string(s, env);
}
static int _get_screen_dpi() {
@@ -674,7 +675,7 @@ static String _get_unique_id() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(godot_io, _getUniqueID);
- return String(env->GetStringUTFChars(s, NULL));
+ return jstring_to_string(s, env);
}
static void _show_vk(const String &p_existing) {
@@ -694,7 +695,7 @@ static String _get_system_dir(int p_dir) {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(godot_io, _getSystemDir, p_dir);
- return String(env->GetStringUTFChars(s, NULL));
+ return jstring_to_string(s, env);
}
static int _get_gles_version_code() {
@@ -891,12 +892,14 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jo
ThreadAndroid::setup_thread();
const char **cmdline = NULL;
+ jstring *j_cmdline = NULL;
int cmdlen = 0;
if (p_cmdline) {
cmdlen = env->GetArrayLength(p_cmdline);
if (cmdlen) {
- cmdline = (const char **)malloc((env->GetArrayLength(p_cmdline) + 1) * sizeof(const char *));
+ cmdline = (const char **)malloc((cmdlen + 1) * sizeof(const char *));
cmdline[cmdlen] = NULL;
+ j_cmdline = (jstring *)malloc(cmdlen * sizeof(jstring));
for (int i = 0; i < cmdlen; i++) {
@@ -904,12 +907,19 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jo
const char *rawString = env->GetStringUTFChars(string, 0);
cmdline[i] = rawString;
+ j_cmdline[i] = string;
}
}
}
Error err = Main::setup("apk", cmdlen, (char **)cmdline, false);
if (cmdline) {
+ if (j_cmdline) {
+ for (int i = 0; i < cmdlen; ++i) {
+ env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
+ }
+ free(j_cmdline);
+ }
free(cmdline);
}
@@ -1313,7 +1323,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, j
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jobject obj, jint p_device, jboolean p_connected, jstring p_name) {
if (os_android) {
- String name = env->GetStringUTFChars(p_name, NULL);
+ String name = jstring_to_string(p_name, env);
os_android->joy_connection_changed(p_device, p_connected, name);
}
}
@@ -1386,7 +1396,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jo
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_singleton(JNIEnv *env, jobject obj, jstring name, jobject p_object) {
- String singname = env->GetStringUTFChars(name, NULL);
+ String singname = jstring_to_string(name, env);
JNISingleton *s = memnew(JNISingleton);
s->set_instance(env->NewGlobalRef(p_object));
jni_singletons[singname] = s;
@@ -1463,21 +1473,21 @@ static const char *get_jni_sig(const String &p_type) {
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jobject obj, jstring path) {
- String js = env->GetStringUTFChars(path, NULL);
+ String js = jstring_to_string(path, env);
return env->NewStringUTF(ProjectSettings::get_singleton()->get(js).operator String().utf8().get_data());
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_method(JNIEnv *env, jobject obj, jstring sname, jstring name, jstring ret, jobjectArray args) {
- String singname = env->GetStringUTFChars(sname, NULL);
+ String singname = jstring_to_string(sname, env);
ERR_FAIL_COND(!jni_singletons.has(singname));
JNISingleton *s = jni_singletons.get(singname);
- String mname = env->GetStringUTFChars(name, NULL);
- String retval = env->GetStringUTFChars(ret, NULL);
+ String mname = jstring_to_string(name, env);
+ String retval = jstring_to_string(ret, env);
Vector<Variant::Type> types;
String cs = "(";
@@ -1486,9 +1496,9 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_method(JNIEnv *env, j
for (int i = 0; i < stringCount; i++) {
jstring string = (jstring)env->GetObjectArrayElement(args, i);
- const char *rawString = env->GetStringUTFChars(string, 0);
- types.push_back(get_jni_type(String(rawString)));
- cs += get_jni_sig(String(rawString));
+ const String rawString = jstring_to_string(string, env);
+ types.push_back(get_jni_type(rawString));
+ cs += get_jni_sig(rawString);
}
cs += ")";
@@ -1511,7 +1521,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *en
int res = env->PushLocalFrame(16);
ERR_FAIL_COND(res != 0);
- String str_method = env->GetStringUTFChars(method, NULL);
+ String str_method = jstring_to_string(method, env);
int count = env->GetArrayLength(params);
Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
@@ -1543,7 +1553,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *
int res = env->PushLocalFrame(16);
ERR_FAIL_COND(res != 0);
- String str_method = env->GetStringUTFChars(method, NULL);
+ String str_method = jstring_to_string(method, env);
int count = env->GetArrayLength(params);
Variant args[VARIANT_ARG_MAX];