Do not keep localRef to object once it is added to an array.

Fix #80
This commit is contained in:
Matthieu Gautier 2024-02-02 15:14:36 +01:00
parent 4a8e966e5d
commit 3f58dc2ecf
3 changed files with 3 additions and 0 deletions

View File

@ -95,6 +95,7 @@ METHOD0(jobjectArray, getIllustrations) {
for (auto illu: illustrations) { for (auto illu: illustrations) {
auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", illu); auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", illu);
env->SetObjectArrayElement(retArray, index++, wrapper); env->SetObjectArrayElement(retArray, index++, wrapper);
env->DeleteLocalRef(wrapper);
} }
return retArray; return retArray;
} CATCH_EXCEPTION(nullptr) } CATCH_EXCEPTION(nullptr)

View File

@ -104,6 +104,7 @@ METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
shared_ptr<kiwix::Bookmark>* handle = new shared_ptr<kiwix::Bookmark>(new kiwix::Bookmark(std::move(bookmark))); shared_ptr<kiwix::Bookmark>* handle = new shared_ptr<kiwix::Bookmark>(new kiwix::Bookmark(std::move(bookmark)));
jobject wrapper = env->NewObject(wrapperClass, initMethod, reinterpret_cast<jlong>(handle)); jobject wrapper = env->NewObject(wrapperClass, initMethod, reinterpret_cast<jlong>(handle));
env->SetObjectArrayElement(retArray, index++, wrapper); env->SetObjectArrayElement(retArray, index++, wrapper);
env->DeleteLocalRef(wrapper);
} }
return retArray; return retArray;
} CATCH_EXCEPTION(nullptr) } CATCH_EXCEPTION(nullptr)

View File

@ -263,6 +263,7 @@ template<> struct JTypeArray<std::string>{
for(size_t index=0; index<length; index++) { for(size_t index=0; index<length; index++) {
auto jElem = c2jni(data[index], env); auto jElem = c2jni(data[index], env);
env->SetObjectArrayElement(array, index, jElem); env->SetObjectArrayElement(array, index, jElem);
env->DeleteLocalRef(jElem);
} }
} }
}; };