This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Examine lifetime of _snapshotCallback in MGLMapSnapshotter startWithQueue:completionHandler: #11801
Copy link
Copy link
Closed
Description
Platform: Darwin
Mapbox SDK version: iOS 4.0.0
Looking at
mapbox-gl-native/platform/darwin/src/MGLMapSnapshotter.mm
Lines 122 to 151 in 65a4ee2
| _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}; | |
| NSError *error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeSnapshotFailed userInfo:userInfo]; | |
| // Dispatch result to origin queue | |
| dispatch_async(queue, ^{ | |
| completion(nil, error); | |
| }); | |
| } else { | |
| #if TARGET_OS_IPHONE | |
| MGLImage *mglImage = [[MGLImage alloc] initWithMGLPremultipliedImage:std::move(image) scale:strongSelf.options.scale]; | |
| #else | |
| MGLImage *mglImage = [[MGLImage alloc] initWithMGLPremultipliedImage:std::move(image)]; | |
| mglImage.size = NSMakeSize(mglImage.size.width / strongSelf.options.scale, | |
| mglImage.size.height / strongSelf.options.scale); | |
| #endif | |
| [strongSelf drawAttributedSnapshot:attributions snapshotImage:mglImage pointForFn:pointForFn queue:queue completionHandler:completion]; | |
| } | |
| _snapshotCallback = NULL; | |
| }); | |
| dispatch_async(queue, ^{ | |
| _mbglMapSnapshotter->snapshot(_snapshotCallback->self()); | |
| }); |
it appears that the _snapshotCallback instance variable can be overwritten, before a previous snapshot can complete. This could potentially result in a NULL deference around line 149.
Let's examine whether _snapshotCallback needs to be an instance variable, and ensure we double check for a valid pointer before calling it.
If it's important it remains an instance variable, consider queueing the assignment (since _mbglMapSnapshotter->snapshot(...) can return before calling the _snapshotCallback).
(In addition, if strongSelf is nil, let's make sure to call completion.)
Activity
ChrisLoer commentedon May 1, 2018
Note that
MapSnapshotter::callbacktakes anActorRefas an argument, andActorRefis a weak pointer that's designed to handle the underlyingActorgoing away (after theActoris torn down, any messages passed to theActorRefare no-ops).ActorRefis copy-able, so you can create one on the stack up above and capture it in the block by value without having to maintain its lifetime separately (like you have to do with theActoritself).ChrisLoer commentedon May 1, 2018
So I'm out of my depth on the iOS/grand-central-dispatch side, but since we're allowing the caller to pass in a queue to
startWithQueue, don't we have to assume that thedispatch_asynccalls might be run on a separate thread from the thread used to create/manage theMGLMapSnapshotter? And if that's the case we couldn't safely use any of these pointers from within the async block.ChrisLoer commentedon May 1, 2018
This definitely looks broken to me, although I still don't know the context for how we expect the
startWithQueuemethod to be used (on a quick glance, it looks like the only part of the iOS SDK where we allow external callers to pass in their own dispatch queue). But anything we're passing into an async block for running on a queue where we don't control the concurrency should either be captured by value or use the Immutable pattern.@julianrex do you want to work on a fix for this together?
cc @ivovandongen @1ec5
ChrisLoer commentedon May 4, 2018
Sorry, I kind of clobbered this issue with #11827 -- when I started writing that up I was thinking of it as a more limited problem, but I think it (and PR #11831) subsumes all of this.