views:

39

answers:

1
-(NSMutableArray *)processResult:(NSArray*)matchArray 
                removeString:(NSString *)removeStr{
    NSString *newLink;
    NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];
    //doing sth to result
    return result;
}

In this scenario, as the result variable will be returned to the caller. And the caller will use this variable through the life time of the app. Is there any way to free the created in this method?

+5  A: 

Go with the memory management guide-lines, return an auto-released instance:

NSMutableArray *result = [NSMutableArray arrayWithCapacity:[matchArray count]];
//doing sth to result
return result;

That way the caller can decide wether he wants to take ownership or not.

Per the guide-lines, the naming implies what the method returns:

  • You own any object you create.
  • You “create” an object using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy).

For your method-name this doesn't apply, thus an auto-released instance should be returned.

Georg Fritzsche