[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
@@ -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 //
|
||||
|
||||
Reference in New Issue
Block a user