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

Fixing malloc without free causing memory leak #395

Merged
merged 1 commit into from
Jul 24, 2019
Merged

Fixing malloc without free causing memory leak #395

merged 1 commit into from
Jul 24, 2019

Conversation

caroaguilar
Copy link
Contributor

Fix memory leak happening every time a user exception is reported. We found this while using the bugsnag-react-native SDK and profiling the app with Xcode Instruments. From there we were able to track this bug down to bugsnag-cocoa SDK.

Screen Shot 2019-07-22 at 3 30 08 PM

By adding the missing free() to the SDK locally we see the leak fixed and not showing in Instruments.

The malloc in question is found here https://github.com/bugsnag/bugsnag-cocoa/blob/master/Source/KSCrash/Source/KSCrash/Recording/BSG_KSCrash.m#L317

Since bsg_kscrash_reportUserException from above file could or could not return, I thought it was better to free the resource in BSG_KSCrashSentry_User.c which is final in call hierarchy.

Changeset

Adding the respectively free() for the malloc

Review

  • This pull request is ready for:
    • Initial review of the intended approach, not yet feature complete
    • Structural review of the classes, functions, and properties modified
    • Final review

Copy link
Contributor

@kattrali kattrali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @caroaguilar! Thank you for the patch. Looking at the code in question, it looks like a leak. This function (bsg_kscrashsentry_reportUserException) is only called from one place - +[BSG_KSCrash reportUserException:...] which is performing the allocation.

- (void)reportUserException:(NSString *)name
reason:(NSString *)reason
originalException:(NSException *)exception
handledState:(NSDictionary *)handledState
appState:(NSDictionary *)appState
callbackOverrides:(NSDictionary *)overrides
metadata:(NSDictionary *)metadata
config:(NSDictionary *)config
discardDepth:(int)depth
terminateProgram:(BOOL)terminateProgram {
const char *cName = [name cStringUsingEncoding:NSUTF8StringEncoding];
const char *cReason = [reason cStringUsingEncoding:NSUTF8StringEncoding];
NSArray *addresses = [exception callStackReturnAddresses];
NSUInteger numFrames = [addresses count];
uintptr_t *callstack = malloc(numFrames * sizeof(*callstack));

As the allocation is not happening in bsg_kscrashsentry_reportUserException, and since use of the array is completed when bsg_kscrashsentry_reportUserException finishes running, it would be easier to follow if the free() was instead in +[BSG_KSCrash reportUserException:...] just after bsg_kscrash_reportUserException is called (around line 334).

bsg_kscrash_reportUserException(cName, cReason,
callstack, numFrames,
[handledState[@"currentSeverity"] UTF8String],
[self encodeAsJSONString:handledState],
[self encodeAsJSONString:overrides],
[self encodeAsJSONString:metadata],
[self encodeAsJSONString:appState],
[self encodeAsJSONString:config],
depth,
terminateProgram);

It would also mean that the free() only needs to happen once, instead of checking for early returns.

The test suite is failing, but it looks like just a missing semicolon after free().

@abigailbramble abigailbramble added bug Confirmed bug scheduled Work is starting on this feature/bug labels Jul 23, 2019
@caroaguilar
Copy link
Contributor Author

caroaguilar commented Jul 23, 2019

Hi @kattrali, thank you for your quick response and review!

Makes total sense, already applied the requested changes.

Thanks :)

@kattrali kattrali merged commit ed0e155 into bugsnag:master Jul 24, 2019
kattrali added a commit that referenced this pull request Jul 30, 2019
kattrali added a commit that referenced this pull request Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug scheduled Work is starting on this feature/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants