-
Notifications
You must be signed in to change notification settings - Fork 130
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
Fixing malloc without free causing memory leak #395
Conversation
There was a problem hiding this 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.
bugsnag-cocoa/Source/KSCrash/Source/KSCrash/Recording/BSG_KSCrash.m
Lines 303 to 317 in c8e5a5a
- (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).
bugsnag-cocoa/Source/KSCrash/Source/KSCrash/Recording/BSG_KSCrash.m
Lines 324 to 334 in c8e5a5a
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()
.
Hi @kattrali, thank you for your quick response and review! Makes total sense, already applied the requested changes. Thanks :) |
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 tobugsnag-cocoa
SDK.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#L317Since
bsg_kscrash_reportUserException
from above file could or could not return, I thought it was better to free the resource inBSG_KSCrashSentry_User.c
which is final in call hierarchy.Changeset
Adding the respectively
free()
for themalloc
Review