diff --git a/include/mbgl/style/sources/raster_dem_source.hpp b/include/mbgl/style/sources/raster_dem_source.hpp index a7738d7f1..7f1e01fd7 100644 --- a/include/mbgl/style/sources/raster_dem_source.hpp +++ b/include/mbgl/style/sources/raster_dem_source.hpp @@ -18,6 +18,7 @@ public: variant urlOrTileset, uint16_t tileSize, std::optional options = std::nullopt); + ~RasterDEMSource() override; bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; protected: diff --git a/include/mbgl/style/sources/raster_source.hpp b/include/mbgl/style/sources/raster_source.hpp index 97e2911e4..15538e7ce 100644 --- a/include/mbgl/style/sources/raster_source.hpp +++ b/include/mbgl/style/sources/raster_source.hpp @@ -16,6 +16,16 @@ public: SourceType sourceType = SourceType::Raster); bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; + + mapbox::base::WeakPtr makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + +protected: + // Allows derived classes (e.g. RasterDEMSource) to invalidate weak pointers + // early in their destructor before their own members are torn down. + void invalidateWeakPtrsEarly() { weakFactory.invalidateWeakPtrs(); } + +private: + mapbox::base::WeakPtrFactory weakFactory{this}; // Must remain last }; template <> diff --git a/include/mbgl/style/sources/tile_source.hpp b/include/mbgl/style/sources/tile_source.hpp index bbeb8de72..3516ee7ef 100644 --- a/include/mbgl/style/sources/tile_source.hpp +++ b/include/mbgl/style/sources/tile_source.hpp @@ -25,8 +25,6 @@ public: void loadDescription(FileSource&) final; - mapbox::base::WeakPtr makeWeakPtr() final { return weakFactory.makeWeakPtr(); } - protected: Mutable createMutable() const noexcept final; virtual void setTilesetOverrides(Tileset& tileset); @@ -34,8 +32,6 @@ protected: private: const variant urlOrTileset; std::unique_ptr req; - mapbox::base::WeakPtrFactory weakFactory{this}; - // Do not add members here, see `WeakPtrFactory` }; template <> diff --git a/include/mbgl/style/sources/vector_source.hpp b/include/mbgl/style/sources/vector_source.hpp index 6e5672c98..4448291fd 100644 --- a/include/mbgl/style/sources/vector_source.hpp +++ b/include/mbgl/style/sources/vector_source.hpp @@ -29,9 +29,12 @@ public: bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; + mapbox::base::WeakPtr makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + private: std::optional maxZoom; std::optional minZoom; + mapbox::base::WeakPtrFactory weakFactory{this}; // Must remain last }; template <> diff --git a/src/mbgl/style/sources/raster_dem_source.cpp b/src/mbgl/style/sources/raster_dem_source.cpp index 805984a7e..68745a048 100644 --- a/src/mbgl/style/sources/raster_dem_source.cpp +++ b/src/mbgl/style/sources/raster_dem_source.cpp @@ -18,6 +18,11 @@ RasterDEMSource::RasterDEMSource(std::string id, : RasterSource(std::move(id), std::move(urlOrTileset_), tileSize, SourceType::RasterDEM), options(std::move(options_)) {} +RasterDEMSource::~RasterDEMSource() { + // Invalidate weak pointers before RasterDEMSource members are destroyed + invalidateWeakPtrsEarly(); +} + bool RasterDEMSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { return mbgl::underlying_type(Tile::Kind::RasterDEM) == mbgl::underlying_type(info->tileKind); }