Skip to content

fetcherCompletion is assign #1565

Closed

Description

@FindCrt

@property(readonly) GTMSessionFetcherCompletionHandler fetcherCompletion;

This property is using default assign, so fetcherCompletion will may be released to case a wild pointer and EXC_BAD_ACCESS crash.

In the file FIRStorageDownloadTask.m, there is a assignment for fetcherCompletion :

_fetcherCompletion = ^(NSData *data, NSError *error) {
      ......
};

And at the end of this scope, there is a fetch:

[self.fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
    weakSelf.fetcherCompletion(data, error);
  }];

Because this fetch is async, when weakSelf.fetcherCompletion calls, the temporary scope in which fetcherCompletion is assigned has gone. fetcherCompletion become a bad memory.

Activity

  1. paulb777 commented on Aug 18, 2018

    @paulb777
    Member

    @ToFind1991 Sorry for the delayed response.

    Are you seeing a demonstrable issue with this code?

    My understanding is that the weakSelf takes care of the issue that you're describing. When fetcherCompletion is deallocated along with its containing object, weakSelf will be set to nil and the function call will be a no-op.

  2. morganchen12 commented on Aug 19, 2018

    @morganchen12
    Contributor

    To clarify the object being deallocated here isn't self, it's the fetcherCompletion closure. This should be copy and not assign.

  3. paulb777 commented on Aug 19, 2018

    @paulb777
    Member

    This stackoverflow answer seems to indicate that while copy is recommended for documentation, it's not functionally necessary when built with ARC.

  4. morganchen12 commented on Aug 19, 2018

    @morganchen12
    Contributor

    strong will do too, but assign is hazardous since it doesn't provide any guarantees for ownership or deallocation.

  5. self-assigned this
    on Aug 20, 2018
  6. added this to the M32 milestone on Aug 21, 2018
  7. andrewehab commented on Jul 4, 2019

    @andrewehab
  8. BlakeBoxberger commented on Aug 20, 2019

    @BlakeBoxberger

    It looks like this issue has somehow re-manifested itself. Even with the copy attribute, I'm getting EXC_BAD_ACCESS on this exact line.

    EDIT: This can be avoided by using .observe() instead of .enqueue(). It should still be properly fixed, but I hope this suggestion helps anyone desperately searching for a fix like I was.

  9. locked and limited conversation to collaborators on Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions