[ios, macos] Fix possible retain cycles in blocks

Prompted by enabling CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF.
This commit is contained in:
Jason Wray
2018-04-24 20:32:43 -04:00
parent 4f7999fd1c
commit 5a147f6335
3 changed files with 33 additions and 16 deletions

View File

@@ -122,8 +122,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64;
_snapshotCallback = std::make_unique<mbgl::Actor<mbgl::MapSnapshotter::Callback>>(*mbgl::Scheduler::GetCurrent(), [=](std::exception_ptr mbglError, mbgl::PremultipliedImage image, mbgl::MapSnapshotter::Attributions attributions, mbgl::MapSnapshotter::PointForFn pointForFn) {
__typeof__(self) strongSelf = weakSelf;
strongSelf.loading = false;
if (mbglError) {
NSString *description = @(mbgl::util::toString(mbglError).c_str());
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: description};
@@ -145,9 +144,13 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64;
}
_snapshotCallback = NULL;
});
dispatch_async(queue, ^{
_mbglMapSnapshotter->snapshot(_snapshotCallback->self());
dispatch_async(queue, ^{
__typeof__(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf->_mbglMapSnapshotter->snapshot(strongSelf->_snapshotCallback->self());
});
}

View File

@@ -4570,6 +4570,8 @@ public:
return;
}
__weak __typeof__(self) weakSelf = self;
// The user location callout view initially points to the user location
// annotation’s implicit (visual) frame, which is offset from the
// annotation’s explicit frame. Now the callout view needs to rendezvous
@@ -4583,10 +4585,16 @@ public:
UIViewAnimationOptionBeginFromCurrentState)
animations:^
{
__typeof__(self) strongSelf = weakSelf;
if ( ! strongSelf)
{
return;
}
calloutView.frame = CGRectOffset(calloutView.frame,
_initialImplicitCalloutViewOffset.x,
_initialImplicitCalloutViewOffset.y);
_initialImplicitCalloutViewOffset = CGPointZero;
strongSelf->_initialImplicitCalloutViewOffset.x,
strongSelf->_initialImplicitCalloutViewOffset.y);
strongSelf->_initialImplicitCalloutViewOffset = CGPointZero;
}
completion:NULL];
}

View File

@@ -93,9 +93,8 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
BOOL _isTouringWorld;
BOOL _isShowingPolygonAndPolylineAnnotations;
BOOL _isShowingAnimatedAnnotation;
// Snapshotter
MGLMapSnapshotter* snapshotter;
MGLMapSnapshotter *_snapshotter;
}
#pragma mark Lifecycle
@@ -185,17 +184,23 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
options.zoomLevel = self.mapView.zoomLevel;
// Create and start the snapshotter
snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MGLMapSnapshot *snapshot, NSError *error) {
__weak __typeof__(self) weakSelf = self;
_snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options];
[_snapshotter startWithCompletionHandler:^(MGLMapSnapshot *snapshot, NSError *error) {
__typeof__(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (error) {
NSLog(@"Could not load snapshot: %@", error.localizedDescription);
} else {
// Set the default name for the file and show the panel.
NSSavePanel *panel = [NSSavePanel savePanel];
panel.nameFieldStringValue = [self.mapView.styleURL.lastPathComponent.stringByDeletingPathExtension stringByAppendingPathExtension:@"png"];
panel.nameFieldStringValue = [strongSelf.mapView.styleURL.lastPathComponent.stringByDeletingPathExtension stringByAppendingPathExtension:@"png"];
panel.allowedFileTypes = [@[(NSString *)kUTTypePNG] arrayByAddingObjectsFromArray:[NSBitmapImageRep imageUnfilteredTypes]];
[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
[panel beginSheetModalForWindow:strongSelf.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
// Write the contents in the new format.
NSURL *fileURL = panel.URL;
@@ -232,7 +237,8 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
}];
}
snapshotter = nil;
strongSelf->_snapshotter = nil;
}];
}
@@ -1179,7 +1185,7 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
return YES;
}
if (menuItem.action == @selector(takeSnapshot:)) {
return !(snapshotter && [snapshotter isLoading]);
return !(_snapshotter && [_snapshotter isLoading]);
}
return NO;
}