Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
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

Closed

Description

@julianrex

Platform: Darwin
Mapbox SDK version: iOS 4.0.0

Looking at

_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.)

/cc @friedbunny @ChrisLoer

Activity

  1. added
    iOS
    macOS
    on Apr 30, 2018
  2. ChrisLoer commented on May 1, 2018

    @ChrisLoer
    Contributor

    Note that MapSnapshotter::callback takes an ActorRef as an argument, and ActorRef is a weak pointer that's designed to handle the underlying Actor going away (after the Actor is torn down, any messages passed to the ActorRef are no-ops). ActorRef is 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 the Actor itself).

  3. ChrisLoer commented on May 1, 2018

    @ChrisLoer
    Contributor

    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 the dispatch_async calls might be run on a separate thread from the thread used to create/manage the MGLMapSnapshotter? And if that's the case we couldn't safely use any of these pointers from within the async block.

  4. ChrisLoer commented on May 1, 2018

    @ChrisLoer
    Contributor

    This definitely looks broken to me, although I still don't know the context for how we expect the startWithQueue method 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

  5. ChrisLoer commented on May 4, 2018

    @ChrisLoer
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugcrashiOSmacOSrefactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions