diff --git a/Source/Android/jni/AndroidCommon/AndroidCommon.cpp b/Source/Android/jni/AndroidCommon/AndroidCommon.cpp index 06da3774c2..d582124102 100644 --- a/Source/Android/jni/AndroidCommon/AndroidCommon.cpp +++ b/Source/Android/jni/AndroidCommon/AndroidCommon.cpp @@ -39,7 +39,11 @@ std::vector JStringArrayToVector(JNIEnv* env, jobjectArray array) result.reserve(size); for (jsize i = 0; i < size; ++i) - result.push_back(GetJString(env, (jstring)env->GetObjectArrayElement(array, i))); + { + jstring str = reinterpret_cast(env->GetObjectArrayElement(array, i)); + result.push_back(GetJString(env, str)); + env->DeleteLocalRef(str); + } return result; } @@ -48,7 +52,11 @@ jobjectArray VectorToJStringArray(JNIEnv* env, std::vector vector) { jobjectArray result = env->NewObjectArray(vector.size(), IDCache::GetStringClass(), nullptr); for (jsize i = 0; i < vector.size(); ++i) - env->SetObjectArrayElement(result, i, ToJString(env, vector[i])); + { + jstring str = ToJString(env, vector[i]); + env->SetObjectArrayElement(result, i, str); + env->DeleteLocalRef(str); + } return result; } @@ -59,7 +67,7 @@ bool IsPathAndroidContent(const std::string& uri) std::string OpenModeToAndroid(std::string mode) { - // The 'b' specifier is not supported. Since we're on POSIX, it's fine to just skip it. + // The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it. if (!mode.empty() && mode.back() == 'b') mode.pop_back(); @@ -92,7 +100,7 @@ std::string OpenModeToAndroid(std::ios_base::openmode mode) if ((mode & t) == t) result += 't'; - // The 'b' specifier is not supported. Since we're on POSIX, it's fine to just skip it. + // The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it. return result; } @@ -123,19 +131,34 @@ jlong GetAndroidContentSizeAndIsDirectory(const std::string& uri) std::string GetAndroidContentDisplayName(const std::string& uri) { JNIEnv* env = IDCache::GetEnvForThread(); - jobject display_name = + + jstring jresult = reinterpret_cast( env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(), - IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri)); - return display_name ? GetJString(env, reinterpret_cast(display_name)) : ""; + IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri))); + + if (!jresult) + return ""; + + std::string result = GetJString(env, jresult); + + env->DeleteLocalRef(jresult); + + return result; } std::vector GetAndroidContentChildNames(const std::string& uri) { JNIEnv* env = IDCache::GetEnvForThread(); - jobject children = env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(), - IDCache::GetContentHandlerGetChildNames(), - ToJString(env, uri), false); - return JStringArrayToVector(env, reinterpret_cast(children)); + + jobjectArray jresult = reinterpret_cast(env->CallStaticObjectMethod( + IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetChildNames(), + ToJString(env, uri), false)); + + std::vector result = JStringArrayToVector(env, jresult); + + env->DeleteLocalRef(jresult); + + return result; } std::vector DoFileSearchAndroidContent(const std::string& directory, @@ -143,10 +166,16 @@ std::vector DoFileSearchAndroidContent(const std::string& directory bool recursive) { JNIEnv* env = IDCache::GetEnvForThread(); - jobject result = env->CallStaticObjectMethod( + + jobjectArray jresult = reinterpret_cast(env->CallStaticObjectMethod( IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(), - ToJString(env, directory), VectorToJStringArray(env, extensions), recursive); - return JStringArrayToVector(env, reinterpret_cast(result)); + ToJString(env, directory), VectorToJStringArray(env, extensions), recursive)); + + std::vector result = JStringArrayToVector(env, jresult); + + env->DeleteLocalRef(jresult); + + return result; } int GetNetworkIpAddress() diff --git a/Source/Android/jni/GameList/GameFileCache.cpp b/Source/Android/jni/GameList/GameFileCache.cpp index 9a3bb49556..c611553aef 100644 --- a/Source/Android/jni/GameList/GameFileCache.cpp +++ b/Source/Android/jni/GameList/GameFileCache.cpp @@ -69,20 +69,8 @@ JNIEXPORT jobject JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_add JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_update( JNIEnv* env, jobject obj, jobjectArray folder_paths, jboolean recursive_scan) { - jsize size = env->GetArrayLength(folder_paths); - - std::vector folder_paths_vector; - folder_paths_vector.reserve(size); - - for (jsize i = 0; i < size; ++i) - { - const auto path = reinterpret_cast(env->GetObjectArrayElement(folder_paths, i)); - folder_paths_vector.push_back(GetJString(env, path)); - env->DeleteLocalRef(path); - } - return GetPointer(env, obj)->Update( - UICommon::FindAllGamePaths(folder_paths_vector, recursive_scan)); + UICommon::FindAllGamePaths(JStringArrayToVector(env, folder_paths), recursive_scan)); } JNIEXPORT jboolean JNICALL