Why Instruments report a leak?
I am developing an iphone app. Instruments reported a leaked object ServiceTypes. Below is the relevant code. Does anyone have any ideas? Thanks a lot for your help.
ServiceTypes *serviceTypes = [[ServiceTypes alloc] init];
if ([userConnection getServiceTypes:serviceTypes]) {
if ([serviceTypes.types len开发者_开发技巧gth] > 0) {
NSArray *array = [[NSArray alloc] initWithArray:[serviceTypes.types componentsSeparatedByString: SERVICE_TYPE_DELIMITOR]];
serviceRequestTypes = [[NSMutableArray alloc] initWithArray:array];
[array release];
}
}
[[self typesTableView] reloadData];
[serviceTypes release];
It doesn't look like serviceTypes
is being leaked. From the code you posted, serviceTypes
is always released at the end of the method, and it doesn't appear to be retain
ed anywhere in your sample. My question is: what happens inside getServiceTypes:
. Does that method retain the serviceTypes
parameter?
One more thing. If serviceRequestTypes
is an instance variable (and it looks like it is), then you may be leaking memory by reassigning it without releasing the existing serviceRequestTypes
object first. You should either rewrite serviceRequestTypes
to be a property and use a synthesized accessor or make sure to release it every time before assigning. If its current value is nil
, no big deal; the release
message will simply be ignored. For example:
[serviceRequestTypes release];
serviceRequestTypes = [[NSMutableArray alloc] initWithArray:[serviceTypes.types componentsSeparatedByString:SERVICE_TYPE_DELIMITER]];
精彩评论