From 4bb3a9c58ca3beb8ab695ae66456cf9c4d9847d4 Mon Sep 17 00:00:00 2001 From: Jesse Crocker Date: Mon, 6 Mar 2017 09:59:00 -0700 Subject: Fix crash when source is dealloced --- platform/darwin/src/MGLComputedShapeSource.mm | 30 +++++++++++++++++-------- src/mbgl/style/sources/custom_vector_source.cpp | 16 ++++--------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/platform/darwin/src/MGLComputedShapeSource.mm b/platform/darwin/src/MGLComputedShapeSource.mm index b4e4a6c54a..7eeea58f6c 100644 --- a/platform/darwin/src/MGLComputedShapeSource.mm +++ b/platform/darwin/src/MGLComputedShapeSource.mm @@ -29,7 +29,10 @@ @property (nonatomic, readonly) uint8_t z; @property (nonatomic, readonly) uint32_t x; @property (nonatomic, readonly) uint32_t y; -@property (nonatomic, readonly, weak) MGLComputedShapeSource *source; +@property (nonatomic, assign) BOOL dataSourceImplementsFeaturesForTile; +@property (nonatomic, assign) BOOL dataSourceImplementsFeaturesForBounds; +@property (nonatomic, weak, nullable) id dataSource; +@property (nonatomic, nullable) mbgl::style::CustomVectorSource *rawSource; - (instancetype)initForSource:(MGLComputedShapeSource*)source z:(uint8_t)z x:(uint32_t)x y:(uint32_t)y; @@ -43,7 +46,11 @@ _x = x; _y = y; _z = z; - _source = source; + _dataSourceImplementsFeaturesForTile = source.dataSourceImplementsFeaturesForTile; + _dataSourceImplementsFeaturesForBounds = source.dataSourceImplementsFeaturesForBounds; + _dataSource = source.dataSource; + mbgl::style::CustomVectorSource *rawSource = (mbgl::style::CustomVectorSource *)source.rawSource; + _rawSource = rawSource; return self; } @@ -53,16 +60,16 @@ } NSArray *> *data; - if(!self.source.dataSource) { + if(!self.dataSource) { data = nil; - } else if(self.source.dataSourceImplementsFeaturesForTile) { - data = [self.source.dataSource featuresInTileAtX:self.x + } else if(self.dataSourceImplementsFeaturesForTile) { + data = [self.dataSource featuresInTileAtX:self.x y:self.y zoomLevel:self.z]; } else { mbgl::CanonicalTileID tileID = mbgl::CanonicalTileID(self.z, self.x, self.y); mbgl::LatLngBounds tileBounds = mbgl::LatLngBounds(tileID); - data = [self.source.dataSource featuresInCoordinateBounds:MGLCoordinateBoundsFromLatLngBounds(tileBounds) + data = [self.dataSource featuresInCoordinateBounds:MGLCoordinateBoundsFromLatLngBounds(tileBounds) zoomLevel:self.z]; } @@ -74,14 +81,19 @@ featureCollection.push_back(geoJsonObject); } const auto geojson = mbgl::GeoJSON{featureCollection}; - dispatch_async(dispatch_get_main_queue(), ^{ - if(![self isCancelled] && self.source.rawSource) { - self.source.rawSource->setTileData(self.z, self.x, self.y, geojson); + dispatch_sync(dispatch_get_main_queue(), ^{ + if(![self isCancelled] && self.rawSource) { + self.rawSource->setTileData(self.z, self.x, self.y, geojson); } }); } } +- (void)cancel { + [super cancel]; + self.rawSource = NULL; +} + @end @implementation MGLComputedShapeSource diff --git a/src/mbgl/style/sources/custom_vector_source.cpp b/src/mbgl/style/sources/custom_vector_source.cpp index 3a12c08f5e..55e7974a83 100644 --- a/src/mbgl/style/sources/custom_vector_source.cpp +++ b/src/mbgl/style/sources/custom_vector_source.cpp @@ -10,27 +10,19 @@ CustomVectorSource::CustomVectorSource(std::string id, GeoJSONOptions options, s } void CustomVectorSource::setTileData(uint8_t z, uint32_t x, uint32_t y, const mapbox::geojson::geojson& geoJSON) { - if(impl != nullptr) { - impl->setTileData(z, x, y, geoJSON); - } + impl->setTileData(z, x, y, geoJSON); } void CustomVectorSource::reloadRegion(mbgl::LatLngBounds bounds, uint8_t z) { - if(impl != nullptr) { - impl->reloadRegion(bounds, z); - } + impl->reloadRegion(bounds, z); } void CustomVectorSource::updateTile(uint8_t z, uint32_t x, uint32_t y) { - if(impl != nullptr) { - impl->updateTile(z, x, y); - } + impl->updateTile(z, x, y); } void CustomVectorSource::reload() { - if(impl != nullptr) { - impl->reload(); - } + impl->reload(); } } // namespace style -- cgit v1.2.1