[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.
This commit is contained in:
committed by
Ivo van Dongen
parent
43f89d6b6a
commit
c30161e4c8
@@ -41,15 +41,19 @@ ActorRef<Renderer> MapRenderer::actor() const {
|
||||
}
|
||||
|
||||
void MapRenderer::schedule(std::weak_ptr<Mailbox> scheduled) {
|
||||
// Create a runnable and schedule it on the gl thread
|
||||
// Create a runnable
|
||||
android::UniqueEnv _env = android::AttachEnv();
|
||||
auto runnable = std::make_unique<MapRendererRunnable>(*_env, std::move(scheduled));
|
||||
|
||||
// Obtain ownership of the peer (gets transferred to the MapRenderer on the JVM for later GC)
|
||||
auto peer = runnable->peer();
|
||||
|
||||
// Queue the event on the Java Peer
|
||||
static auto queueEvent = javaClass.GetMethod<void(
|
||||
jni::Object<MapRendererRunnable>)>(*_env, "queueEvent");
|
||||
javaPeer->Call(*_env, queueEvent, runnable->getPeer());
|
||||
javaPeer->Call(*_env, queueEvent, *peer);
|
||||
|
||||
// Release the object as it will be destroyed on GC of the Java Peer
|
||||
// Release the c++ peer as it will be destroyed on GC of the Java Peer
|
||||
runnable.release();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,13 @@ namespace android {
|
||||
MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::weak_ptr<Mailbox> mailbox_)
|
||||
: mailbox(std::move(mailbox_)) {
|
||||
|
||||
// Create the Java peer
|
||||
// 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 = SeizeGenericWeakRef(env, jni::Object<MapRendererRunnable>(jni::NewWeakGlobalRef(env, instance.Get()).release()));
|
||||
javaPeer = instance.NewGlobalRef(env);
|
||||
}
|
||||
|
||||
MapRendererRunnable::~MapRendererRunnable() = default;
|
||||
@@ -21,8 +23,8 @@ void MapRendererRunnable::run(jni::JNIEnv&) {
|
||||
Mailbox::maybeReceive(mailbox);
|
||||
}
|
||||
|
||||
jni::Object<MapRendererRunnable> MapRendererRunnable::getPeer() {
|
||||
return *javaPeer;
|
||||
jni::UniqueObject<MapRendererRunnable> MapRendererRunnable::peer() {
|
||||
return std::move(javaPeer);
|
||||
}
|
||||
|
||||
// Static methods //
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include "jni/generic_global_ref_deleter.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
|
||||
@@ -39,10 +37,11 @@ public:
|
||||
|
||||
void run(jni::JNIEnv&);
|
||||
|
||||
jni::Object<MapRendererRunnable> getPeer();
|
||||
// Transfers ownership of the Peer object to the caller
|
||||
jni::UniqueObject<MapRendererRunnable> peer();
|
||||
|
||||
private:
|
||||
GenericUniqueWeakObject<MapRendererRunnable> javaPeer;
|
||||
jni::UniqueObject<MapRendererRunnable> javaPeer;
|
||||
std::weak_ptr<Mailbox> mailbox;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user