Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXC_BAD_ACCESS in Amplitude.m due to dereferencing nil after refreshing dynamic config #288

Closed
jaltreuter opened this issue Oct 5, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@jaltreuter
Copy link
Contributor

Expected Behavior

If Amplitude is deallocated while dynamic config is being refreshed, the completion block should no-op.

Current Behavior

If Amplitude is deallocated while dynamic config is being refreshed, the completion block will dereference nil and crash the app.

Possible Solution

This is a snippet of the existing code in Amplitude.m:

- (void)refreshDynamicConfig {
    if (self.useDynamicConfig) {
        __weak typeof(self) weakSelf = self;
        [[AMPConfigManager sharedInstance] refresh:^{
            __strong typeof(self) strongSelf = weakSelf;
            strongSelf->_serverUrl = [AMPConfigManager sharedInstance].ingestionEndpoint;
        }];
    }
}

The block avoids a retain cycle by creating a weak reference to self, but incorrectly uses that reference. If weakSelf becomes nil, the block will then dereference that value and crash. It should be replaced with

- (void)refreshDynamicConfig {
    if (self.useDynamicConfig) {
        __weak typeof(self) weakSelf = self;
        [[AMPConfigManager sharedInstance] refresh:^{
            __strong typeof(self) strongSelf = weakSelf;
            if strongSelf == nil {
                return;
            }
            strongSelf->_serverUrl = [AMPConfigManager sharedInstance].ingestionEndpoint;
        }];
    }
}

Steps to Reproduce

  1. Create an instance of Amplitude
  2. Set useDynamicConfig to true
  3. Call initializeApiKey
  4. Remove all references to this instance of Amplitude

Environment

  • SDK Version: 7.1.0
  • Device: iPhone XS Simulator
  • OS Version: iOS 12.0
@jaltreuter jaltreuter added the bug Something isn't working label Oct 5, 2020
@jooohhn jooohhn self-assigned this Oct 5, 2020
jaltreuter pushed a commit to jaltreuter/Amplitude-iOS that referenced this issue Oct 5, 2020
jaltreuter pushed a commit to jaltreuter/Amplitude-iOS that referenced this issue Oct 5, 2020
@jooohhn jooohhn removed their assignment Oct 6, 2020
jooohhn pushed a commit that referenced this issue Oct 7, 2020
Abort execution of completion block after dynamic config refresh completes if self is now nil

Co-authored-by: Jamie Altreuter <jamie@aisense.com>
@haoliu-amp
Copy link
Contributor

Merged. Thanks!

github-actions bot pushed a commit that referenced this issue Oct 20, 2020
## [7.1.1](v7.1.0...v7.1.1) (2020-10-20)

### Bug Fixes

* **buildsettings:** Remove override for GCC_WARN_INHIBIT_ALL_WARNINGS ([#302](#302)) ([0e55297](0e55297))
* **deprecation warnings:** Fix deprecation warnings ([#301](#301)) ([e7b0e6e](e7b0e6e)), closes [/github.com//issues/250#issuecomment-655224554](https://github.com//github.com/amplitude/Amplitude-iOS/issues/250/issues/issuecomment-655224554)
* **deprecations:** Use DEPRECATED_MSG_ATTRIBUTE instead of notes ([#305](#305)) ([f501c6c](f501c6c))
* nil dynamic config refresh crash ([#288](#288)) ([#289](#289)) ([9dc896d](9dc896d))
* Swift UserId and DeviceId setter ([#299](#299)) ([b7c0f90](b7c0f90))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants