Two Objective-C Memory Management Issues: Is Leak Detection Always Right? Why does the abstract work, but not released?

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) {
    //Do stuff to expressionDescription
    }

    [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) {
    //Do stuff to expressionDescription
    }

    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) {
    //Do stuff to expressionDescription
    }

    [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, , , , . , , "", "", . , , , , .

, "":

: , , .

: , .

: , , .

+3
3

, . , Xcode ( , )

. - . , B & A ( , ).

, . , , , , , , .

, ,

  • - , . , iOS, , - , " " - iOS.

  • allocSOMETHING newSOMETHING autorelease. , , , ( ).

, Build and Analyze .

: newSOMETHING

+5

, autorelease .

  • . , .
  • , . , " " , .
  • , autorelease , ( ). , . , , , , . - , , .

. , , . , 99% , , , .

, , - , , , . , , , .

+2

?

. : "" . , , , . , API- Cocoa/Cocoa Touch, . , , -[NSTimer invalidate] , API.

Zombies ( AFAICT, iOS), , : ( , ). .

+1

Source: https://habr.com/ru/post/1782008/


All Articles