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

[PLAT-9307] add autorelease pool to fix memory leak #1507

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletions Bugsnag/Helpers/BSGAppHangDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,46 +122,48 @@ - (void)detectAppHangs {
NSThread.currentThread.name = @"com.bugsnag.app-hang-detector";

while (!self.shouldStop) {
if (dispatch_semaphore_wait(self.processingStarted, DISPATCH_TIME_FOREVER) != 0) {
bsg_log_err(@"BSGAppHangDetector: dispatch_semaphore_wait failed unexpectedly");
return;
}

const dispatch_time_t deadline = self.processingDeadline;

if (dispatch_semaphore_wait(self.processingFinished, deadline) == 0) {
// Run loop finished within the deadline
continue;
}

BOOL shouldReportAppHang = YES;

if (dispatch_time(DISPATCH_TIME_NOW, 0) > dispatch_time(deadline, 1 * NSEC_PER_SEC)) {
// If this thread has woken up long after the deadline, the app may have been suspended.
bsg_log_debug(@"Ignoring potential false positive app hang");
shouldReportAppHang = NO;
}

@autoreleasepool {
if (dispatch_semaphore_wait(self.processingStarted, DISPATCH_TIME_FOREVER) != 0) {
bsg_log_err(@"BSGAppHangDetector: dispatch_semaphore_wait failed unexpectedly");
return;
}

const dispatch_time_t deadline = self.processingDeadline;

if (dispatch_semaphore_wait(self.processingFinished, deadline) == 0) {
// Run loop finished within the deadline
continue;
}

BOOL shouldReportAppHang = YES;

if (dispatch_time(DISPATCH_TIME_NOW, 0) > dispatch_time(deadline, 1 * NSEC_PER_SEC)) {
// If this thread has woken up long after the deadline, the app may have been suspended.
bsg_log_debug(@"Ignoring potential false positive app hang");
shouldReportAppHang = NO;
}

#if defined(DEBUG) && DEBUG
if (shouldReportAppHang && bsg_ksmachisBeingTraced()) {
bsg_log_debug(@"Ignoring app hang because debugger is attached");
shouldReportAppHang = NO;
}
if (shouldReportAppHang && bsg_ksmachisBeingTraced()) {
bsg_log_debug(@"Ignoring app hang because debugger is attached");
shouldReportAppHang = NO;
}
#endif

if (shouldReportAppHang && !bsg_runContext->isForeground && !self.delegate.configuration.reportBackgroundAppHangs) {
bsg_log_debug(@"Ignoring app hang because app is in the background");
shouldReportAppHang = NO;
}

if (shouldReportAppHang) {
[self appHangDetected];
}

dispatch_semaphore_wait(self.processingFinished, DISPATCH_TIME_FOREVER);

if (shouldReportAppHang) {
[self appHangEnded];

if (shouldReportAppHang && !bsg_runContext->isForeground && !self.delegate.configuration.reportBackgroundAppHangs) {
bsg_log_debug(@"Ignoring app hang because app is in the background");
shouldReportAppHang = NO;
}

if (shouldReportAppHang) {
[self appHangDetected];
}

dispatch_semaphore_wait(self.processingFinished, DISPATCH_TIME_FOREVER);

if (shouldReportAppHang) {
[self appHangEnded];
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Fix memory leak in the app hang detection code.
[#1507](https://github.com/bugsnag/bugsnag-cocoa/pull/1507)

* Truncate additional data to reduce number of oversized payloads.
[#1501](https://github.com/bugsnag/bugsnag-cocoa/pull/1501)

Expand Down