diff --git a/CHANGELOG.md b/CHANGELOG.md index 31b7f4cad..1d0ecf306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +## 5.X.X (TBD) + +### Bug Fixes + +* Prevent potential crash in session delivery during app teardown [#308](https://github.com/bugsnag/bugsnag-cocoa/pull/308) + ## 5.16.3 (14 Aug 2018) ### Bug Fixes diff --git a/Source/BugsnagApiClient.m b/Source/BugsnagApiClient.m index a3ac73c93..8550286c2 100644 --- a/Source/BugsnagApiClient.m +++ b/Source/BugsnagApiClient.m @@ -135,6 +135,10 @@ - (NSMutableURLRequest *)prepareRequest:(NSURL *)url return request; } +- (void)dealloc { + [self.sendQueue cancelAllOperations]; +} + @end @implementation BSGDelayOperation diff --git a/Source/BugsnagSessionTrackingApiClient.m b/Source/BugsnagSessionTrackingApiClient.m index 994b94db2..b140ecb7d 100644 --- a/Source/BugsnagSessionTrackingApiClient.m +++ b/Source/BugsnagSessionTrackingApiClient.m @@ -19,8 +19,11 @@ - (NSOperation *)deliveryOperation { } - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { + NSString *apiKey = [self.config.apiKey copy]; + NSURL *sessionURL = [self.config.sessionURL copy]; + [self.sendQueue addOperationWithBlock:^{ - if (!self.config.apiKey) { + if (!apiKey) { bsg_log_err(@"No API key set. Refusing to send sessions."); return; } @@ -41,16 +44,16 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { if (sessionCount > 0) { NSDictionary *HTTPHeaders = @{ @"Bugsnag-Payload-Version": @"1.0", - @"Bugsnag-API-Key": self.config.apiKey, + @"Bugsnag-API-Key": apiKey, @"Bugsnag-Sent-At": [BSG_RFC3339DateTool stringFromDate:[NSDate new]] }; [self sendData:payload withPayload:[payload toJson] - toURL:self.config.sessionURL + toURL:sessionURL headers:HTTPHeaders onCompletion:^(id data, BOOL success, NSError *error) { if (success && error == nil) { - bsg_log_info(@"Sent %lu sessions to Bugsnag", (unsigned long)sessionCount); + bsg_log_info(@"Sent %lu sessions to Bugsnag", (unsigned long) sessionCount); for (NSString *fileId in fileIds) { [store deleteFileWithId:fileId];