Add weak pointer management to RasterSource and derived classes (#3726)

This commit is contained in:
Bart Louwers
2025-08-26 19:31:25 +02:00
committed by GitHub
parent 77c251bd1d
commit 28e73cade3
5 changed files with 19 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ public:
variant<std::string, Tileset> urlOrTileset,
uint16_t tileSize,
std::optional<RasterDEMOptions> options = std::nullopt);
~RasterDEMSource() override;
bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override;
protected:

View File

@@ -16,6 +16,16 @@ public:
SourceType sourceType = SourceType::Raster);
bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override;
mapbox::base::WeakPtr<Source> 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<Source> weakFactory{this}; // Must remain last
};
template <>

View File

@@ -25,8 +25,6 @@ public:
void loadDescription(FileSource&) final;
mapbox::base::WeakPtr<Source> makeWeakPtr() final { return weakFactory.makeWeakPtr(); }
protected:
Mutable<Source::Impl> createMutable() const noexcept final;
virtual void setTilesetOverrides(Tileset& tileset);
@@ -34,8 +32,6 @@ protected:
private:
const variant<std::string, Tileset> urlOrTileset;
std::unique_ptr<AsyncRequest> req;
mapbox::base::WeakPtrFactory<Source> weakFactory{this};
// Do not add members here, see `WeakPtrFactory`
};
template <>

View File

@@ -29,9 +29,12 @@ public:
bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override;
mapbox::base::WeakPtr<Source> makeWeakPtr() override { return weakFactory.makeWeakPtr(); }
private:
std::optional<float> maxZoom;
std::optional<float> minZoom;
mapbox::base::WeakPtrFactory<Source> weakFactory{this}; // Must remain last
};
template <>