From a3af56f9d2686a88a54128313944c3f62f67632c Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Wed, 18 Mar 2020 23:01:16 +0200 Subject: Destroy snapshotter on the thread it was created --- platform/android/src/snapshotter/map_snapshotter.cpp | 16 +++++++++++++++- platform/android/src/snapshotter/map_snapshotter.hpp | 7 +++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 9027f56fce..7006c2774f 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -33,6 +33,8 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, return; } + weakScheduler = mbgl::Scheduler::GetCurrent()->makeWeakPtr(); + jFileSource = FileSource::getNativePeer(_env, _jFileSource); auto size = mbgl::Size { static_cast(width), static_cast(height) }; @@ -61,7 +63,19 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, activateFilesource(_env); } -MapSnapshotter::~MapSnapshotter() = default; +MapSnapshotter::~MapSnapshotter() { + auto guard = weakScheduler.lock(); + if (weakScheduler && weakScheduler.get() != mbgl::Scheduler::GetCurrent()) { + snapshotter->cancel(); + std::shared_ptr shared = std::move(snapshotter); + weakScheduler->schedule([s = std::move(shared)] { + (void)s; + }); + } else { + snapshotter.reset(); + } + vm = nullptr; +} void MapSnapshotter::start(JNIEnv& env) { MBGL_VERIFY_THREAD(tid); diff --git a/platform/android/src/snapshotter/map_snapshotter.hpp b/platform/android/src/snapshotter/map_snapshotter.hpp index ac84339a62..1da4f8275b 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -3,7 +3,9 @@ #include #include #include + #include +#include #include "../file_source.hpp" #include "../geometry/lat_lng_bounds.hpp" @@ -35,7 +37,7 @@ public: jni::jboolean showLogo, const jni::String& localIdeographFontFamily); - ~MapSnapshotter(); + virtual ~MapSnapshotter(); void setStyleUrl(JNIEnv&, const jni::String& styleURL); @@ -71,11 +73,12 @@ private: float pixelRatio; bool showLogo; - std::unique_ptr snapshotter; FileSource *jFileSource; void activateFilesource(JNIEnv&); void deactivateFilesource(JNIEnv&); bool activatedFilesource = false; + std::unique_ptr snapshotter; + mapbox::base::WeakPtr weakScheduler; }; } // namespace android -- cgit v1.2.1