Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Destroy snapshotter on the thread it was created
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov authored and kevin committed Mar 20, 2020
1 parent 5910dd5 commit eeea3af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 15 additions & 1 deletion platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) };

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions platform/android/src/snapshotter/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -35,7 +37,7 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
jni::jboolean showLogo,
const jni::String& localIdeographFontFamily);

~MapSnapshotter();
virtual ~MapSnapshotter();

void setStyleUrl(JNIEnv&, const jni::String& styleURL);

Expand Down Expand Up @@ -71,11 +73,12 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
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
Expand Down

0 comments on commit eeea3af

Please sign in to comment.