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.
50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <mbgl/actor/mailbox.hpp>
|
|
#include <mbgl/actor/scheduler.hpp>
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include <jni/jni.hpp>
|
|
|
|
namespace mbgl {
|
|
namespace android {
|
|
|
|
/**
|
|
* The MapRendererRunnable is a peer class that encapsulates
|
|
* a scheduled mailbox in a Java Runnable so it can be
|
|
* scheduled on the map renderer thread.
|
|
*
|
|
*/
|
|
class MapRendererRunnable {
|
|
public:
|
|
|
|
static constexpr auto Name() { return "com/mapbox/mapboxsdk/maps/renderer/MapRendererRunnable"; };
|
|
|
|
static jni::Class<MapRendererRunnable> javaClass;
|
|
|
|
static void registerNative(jni::JNIEnv&);
|
|
|
|
MapRendererRunnable(jni::JNIEnv&, std::weak_ptr<Mailbox>);
|
|
|
|
// Only for jni registration, unused
|
|
MapRendererRunnable(jni::JNIEnv&) {
|
|
assert(false);
|
|
}
|
|
|
|
~MapRendererRunnable();
|
|
|
|
void run(jni::JNIEnv&);
|
|
|
|
// Transfers ownership of the Peer object to the caller
|
|
jni::UniqueObject<MapRendererRunnable> peer();
|
|
|
|
private:
|
|
jni::UniqueObject<MapRendererRunnable> javaPeer;
|
|
std::weak_ptr<Mailbox> mailbox;
|
|
};
|
|
|
|
} // namespace android
|
|
} // namespace mbgl
|