diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-03-18 23:01:16 +0200 |
---|---|---|
committer | kevin <kevin.li@mapbox.com> | 2020-03-24 10:39:43 +0800 |
commit | a3af56f9d2686a88a54128313944c3f62f67632c (patch) | |
tree | e41b28a1dbb33b1a149a1b09d51f6fbd8cf08a07 | |
parent | 14d5984d8704aa4f10a58001392fadd2a99013db (diff) | |
download | qtlocation-mapboxgl-a3af56f9d2686a88a54128313944c3f62f67632c.tar.gz |
Destroy snapshotter on the thread it was created
-rw-r--r-- | platform/android/src/snapshotter/map_snapshotter.cpp | 16 | ||||
-rw-r--r-- | 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<uint32_t>(width), static_cast<uint32_t>(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<mbgl::MapSnapshotter> 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 <jni/jni.hpp> #include <mbgl/map/map_snapshotter.hpp> #include <mbgl/util/util.hpp> + #include <memory> +#include <mapbox/weak.hpp> #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<mbgl::MapSnapshotter> snapshotter; FileSource *jFileSource; void activateFilesource(JNIEnv&); void deactivateFilesource(JNIEnv&); bool activatedFilesource = false; + std::unique_ptr<mbgl::MapSnapshotter> snapshotter; + mapbox::base::WeakPtr<mbgl::Scheduler> weakScheduler; }; } // namespace android |