Files
MapLibreParticleLab/platform/android/src/map_renderer_runnable.cpp
Pablo Guardiola b5140cb342 [android] Cherry picks to agua (#10442)
* [android] fix map snapshotter marker example

The reference to the MapSnapshotter needs to be held for the duration of the snapshot, otherwise it might be GC'd

* [android] MapRendererRunnable - avoid weak reference table overflow

Apparently on some devices the weak reference table is limited (numbers around 52000). Even though we don't use that many weak references, when GC is not called for a while they can stack up and a crash will occur before the GC has had the time to clear the references. The C++ peer now holds on to a global ref (strong) which can be obtained to queue the java peer and then release automatically so that the GC can take over after the runnable has been executed.
2017-11-10 12:56:06 +01:00

52 lines
1.6 KiB
C++

#include "map_renderer_runnable.hpp"
#include <mbgl/util/logging.hpp>
namespace mbgl {
namespace android {
MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::weak_ptr<Mailbox> mailbox_)
: mailbox(std::move(mailbox_)) {
// Create the Java peer and hold on to a global reference
// Not using a weak reference here as this might oerflow
// the weak reference table on some devices
jni::UniqueLocalFrame frame = jni::PushLocalFrame(env, 5);
static auto constructor = javaClass.GetConstructor<jlong>(env);
auto instance = javaClass.New(env, constructor, reinterpret_cast<jlong>(this));
javaPeer = instance.NewGlobalRef(env);
}
MapRendererRunnable::~MapRendererRunnable() = default;
void MapRendererRunnable::run(jni::JNIEnv&) {
Mailbox::maybeReceive(mailbox);
}
jni::UniqueObject<MapRendererRunnable> MapRendererRunnable::peer() {
return std::move(javaPeer);
}
// Static methods //
jni::Class<MapRendererRunnable> MapRendererRunnable::javaClass;
void MapRendererRunnable::registerNative(jni::JNIEnv& env) {
// Lookup the class
MapRendererRunnable::javaClass = *jni::Class<MapRendererRunnable>::Find(env).NewGlobalRef(env).release();
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
jni::RegisterNativePeer<MapRendererRunnable>(
env,
MapRendererRunnable::javaClass,
"nativePtr",
std::make_unique<MapRendererRunnable, JNIEnv&>,
"nativeInitialize",
"finalize",
METHOD(&MapRendererRunnable::run, "run"));
}
} // namespace android
} // namespace mbgl