Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #3891 - capture the callback by copy, avoid GC, and declare…
Browse files Browse the repository at this point in the history
… a new JNIEnv
  • Loading branch information
zugaldia committed Feb 19, 2016
1 parent 79f918e commit debcaa2
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1720,36 +1720,39 @@ void JNICALL createOfflineRegion(JNIEnv *env, jobject obj, jlong defaultFileSour
// Metadata
mbgl::OfflineRegionMetadata metadata;

createCallback = reinterpret_cast<jobject>(env->NewGlobalRef(createCallback));

// Launch createCallback
mbgl::DefaultFileSource *defaultFileSource = reinterpret_cast<mbgl::DefaultFileSource *>(defaultFileSourcePtr);
defaultFileSource->createOfflineRegion(definition, metadata, [&env, &createCallback] (std::exception_ptr error, mbgl::optional<mbgl::OfflineRegion> region) {
defaultFileSource->createOfflineRegion(definition, metadata, [createCallback] (std::exception_ptr error, mbgl::optional<mbgl::OfflineRegion> region) {

// Reattach, the callback comes from a different thread
jboolean renderDetach = attach_jni_thread(theJVM, &env, "Offline Thread");
JNIEnv *env2;
jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Offline Thread");
if (renderDetach) {
mbgl::Log::Debug(mbgl::Event::JNI, "Attached.");
}

if (error) {
std::string message = mbgl::util::toString(error);
env->CallVoidMethod(createCallback, createOnErrorMethodId, std_string_to_jstring(env, message));
env2->CallVoidMethod(createCallback, createOnErrorMethodId, std_string_to_jstring(env2, message));
} else if (region) {
mbgl::Log::Error(mbgl::Event::JNI, "Region created.");
jobject jRegion = env->NewObject(offlineRegionClass, offlineRegionConstructorId, region->getID());
// env->SetLongField(jRegion, offlineRegionIdId, region->getID());
jobject jRegion = env2->NewObject(offlineRegionClass, offlineRegionConstructorId, region->getID());
// env2->SetLongField(jRegion, offlineRegionIdId, region->getID());

if (env->ExceptionCheck()) {
if (env2->ExceptionCheck()) {
mbgl::Log::Error(mbgl::Event::JNI, "Exception.");
env->ExceptionDescribe();
env2->ExceptionDescribe();
} else {
// This currently causes an exception on Android
mbgl::Log::Error(mbgl::Event::JNI, "Triggering Java callback.");
env->CallVoidMethod(createCallback, createOnCreateMethodId, jRegion);
env2->CallVoidMethod(createCallback, createOnCreateMethodId, jRegion);
}
}

// Detach when we're done
detach_jni_thread(theJVM, &env, renderDetach);
detach_jni_thread(theJVM, &env2, renderDetach);
});
}

Expand Down

0 comments on commit debcaa2

Please sign in to comment.