diff options
author | Igor Tarasov <igor.tarasov@mapbox.com> | 2019-09-17 15:24:28 +0300 |
---|---|---|
committer | Igor Tarasov <igor.tarasov@mapbox.com> | 2019-09-17 15:24:28 +0300 |
commit | 751b394a0e5081e5af0c78dcaf4ba49643dbf89e (patch) | |
tree | 5698b7b6644be6bde2904b2f2c66803dc65a5db0 | |
parent | 08830a7defd414509bb3b094e1e3a21272dfb112 (diff) | |
download | qtlocation-mapboxgl-751b394a0e5081e5af0c78dcaf4ba49643dbf89e.tar.gz |
[android] Style source - use weak pointer.
-rw-r--r-- | platform/android/src/style/sources/source.cpp | 10 | ||||
-rw-r--r-- | platform/android/src/style/sources/source.hpp | 12 |
2 files changed, 10 insertions, 12 deletions
diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp index 8f4e631e9a..c067c5d1c4 100644 --- a/platform/android/src/style/sources/source.cpp +++ b/platform/android/src/style/sources/source.cpp @@ -5,7 +5,6 @@ #include <jni/jni.hpp> #include <mbgl/style/style.hpp> -#include <mbgl/util/logging.hpp> // Java -> C++ conversion #include <mbgl/style/conversion/source.hpp> @@ -98,12 +97,13 @@ namespace android { throw std::runtime_error("Cannot add source twice"); } + // Save reference before transferring ownership + nonOwnedSource_ = ownedSource_->makeWeakPtr(); + // Add peer to core source + ownedSource_->peer = std::unique_ptr<Source>(this); // Add source to map and release ownership map.getStyle().addSource(std::move(ownedSource_)); - // Add peer to core source - source(env)->peer = std::unique_ptr<Source>(this); - // Add strong reference to java source javaPeer = jni::NewGlobal(env, obj); @@ -113,7 +113,7 @@ namespace android { bool Source::removeFromMap(JNIEnv& env, const jni::Object<Source>&, mbgl::Map& map) { // Cannot remove if not attached yet if (ownedSource_) { - throw std::runtime_error("Cannot remove detached source"); + throw std::runtime_error("Cannot remove detached source from map"); } // Remove the source from the map and take ownership diff --git a/platform/android/src/style/sources/source.hpp b/platform/android/src/style/sources/source.hpp index 5bab79bca4..061529fb9f 100644 --- a/platform/android/src/style/sources/source.hpp +++ b/platform/android/src/style/sources/source.hpp @@ -52,16 +52,16 @@ protected: } inline void source(mbgl::style::Source *coreSource) noexcept { - coreSource_ = coreSource->makeWeakPtr(); + nonOwnedSource_ = coreSource->makeWeakPtr(); } - inline mbgl::style::Source *source(jni::JNIEnv& env) { + inline mbgl::style::Source* source(jni::JNIEnv& env) { if (ownedSource_) return ownedSource_.get(); - if (!coreSource_) { + if (!nonOwnedSource_) { jni::ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "This source got invalidated after the style change"); } - return coreSource_.get(); + return nonOwnedSource_.get(); } // Set when the source is added to a map. @@ -71,10 +71,8 @@ protected: AndroidRendererFrontend* rendererFrontend { nullptr }; private: - // Set on newly created sources until added to the map. std::unique_ptr<mbgl::style::Source> ownedSource_ { nullptr }; - - mapbox::base::WeakPtr<mbgl::style::Source> coreSource_; + mapbox::base::WeakPtr<mbgl::style::Source> nonOwnedSource_; }; } // namespace android |