From a0b7d4f5776fd8e3bbd1cd63316a4ba0eab3f58e Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Fri, 20 Mar 2020 10:27:00 +0200 Subject: Release snapshotter pointer to deleting lambda --- .../android/src/snapshotter/map_snapshotter.cpp | 36 ++++++++++------------ .../android/src/snapshotter/map_snapshotter.hpp | 15 +++------ 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index c72d657fc5..812bd414eb 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -1,6 +1,5 @@ #include "map_snapshotter.hpp" -#include #include #include #include @@ -13,20 +12,6 @@ namespace mbgl { namespace android { -MapSnapshotter::DeleteOnThread::DeleteOnThread() = default; -MapSnapshotter::DeleteOnThread::DeleteOnThread(mapbox::base::WeakPtr weakScheduler_) - : weakScheduler(weakScheduler_) {} - -void MapSnapshotter::DeleteOnThread::operator()(mbgl::MapSnapshotter* p) const { - auto guard = weakScheduler.lock(); - if (weakScheduler && weakScheduler.get() != mbgl::Scheduler::GetCurrent()) { - weakScheduler->schedule( - [ptr = std::shared_ptr(p, DeleteOnThread(weakScheduler))] { (void)ptr; }); - } else { - delete p; - } -} - MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, const jni::Object& _obj, const jni::Object& _jFileSource, @@ -48,20 +33,20 @@ 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) }; showLogo = _showLogo; // Create the core snapshotter - snapshotter = std::unique_ptr( - new mbgl::MapSnapshotter(size, + snapshotter = std::make_unique(size, pixelRatio, mbgl::android::FileSource::getSharedResourceOptions(_env, _jFileSource), *this, _localIdeographFontFamily ? jni::Make(_env, _localIdeographFontFamily) - : optional{}), - DeleteOnThread(mbgl::Scheduler::GetCurrent()->makeWeakPtr())); + : optional{}); if (position) { snapshotter->setCameraOptions(CameraPosition::getCameraOptions(_env, position, pixelRatio)); @@ -78,7 +63,18 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, } } -MapSnapshotter::~MapSnapshotter() = default; +MapSnapshotter::~MapSnapshotter() { + auto guard = weakScheduler.lock(); + if (weakScheduler && weakScheduler.get() != mbgl::Scheduler::GetCurrent()) { + snapshotter->cancel(); + weakScheduler->schedule([ptr = snapshotter.release()]() mutable { + if (ptr) { + delete ptr; + ptr = 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 99c8b6d2df..c7c25faab4 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -3,6 +3,8 @@ #include #include #include +#include + #include #include "../file_source.hpp" @@ -62,16 +64,6 @@ public: void onDidFinishLoadingStyle() override; void onStyleImageMissing(const std::string&) override; -private: - struct DeleteOnThread { - DeleteOnThread(); - explicit DeleteOnThread(mapbox::base::WeakPtr); - void operator()(mbgl::MapSnapshotter* p) const; - - private: - mapbox::base::WeakPtr weakScheduler; - }; - private: MBGL_STORE_THREAD(tid); @@ -85,7 +77,8 @@ private: void activateFilesource(JNIEnv&); void deactivateFilesource(JNIEnv&); bool activatedFilesource = false; - std::unique_ptr snapshotter; + mapbox::base::WeakPtr weakScheduler; + std::unique_ptr snapshotter; }; } // namespace android -- cgit v1.2.1