I just discovered the following: As I expected, releasing my object before I return it will cause the application to crash:
+ (NSString *)descriptionOfExpression:(NSArray *)anExpression {
NSMutableString *expressionDescription;
expressionDescription = [[NSMutableString alloc] init];
for (id object in anExpression) {
}
[expressionDescription release];
return expressionDescription;
}
However, I did not expect the following to result in a memory leak:
+ (NSString *)descriptionOfExpression:(NSArray *)anExpression {
NSMutableString *expressionDescription;
expressionDescription = [[NSMutableString alloc] init];
for (id object in anExpression) {
}
return expressionDescription;
[expressionDescription release];
}
Ultimately, I decided to do this, instead:
+ (NSString *)descriptionOfExpression:(NSArray *)anExpression {
NSMutableString *expressionDescription;
expressionDescription = [[NSMutableString alloc] init];
for (id object in anExpression) {
}
[expressionDescription autorelease];
return expressionDescription;
}
I understand why its auto-implementation works, but how is the leak caused by the release after returning the value?
My second question is very important: are memory leak detection systems constantly detected?
, , "" " " XCode, , , , . , , "", "", . , , , , .
, "":
: , , .
: , .
: , , .