Search code examples
objective-ciosautomatic-ref-countinginstruments

Memory leak with ARC when returning a parameter object


I have a strange (looks to me) and simple memory leak situation using ARC (automatic reference counting). I am working with iOS code, but I think it should apply to Objective-C in general.


I have a following function that returns the object assigned as a parameter after checking the properties of the parameter object.

- (id) returnParam:(id) obj
{
    // Do whatever filtering needed. Return nil for invalid input.

    return (NSNumber *)obj;
}

If I call this method in a loop as follows, I can see that the allocated memory keeps increasing until the loop ends in Instruments.

for(int i = 0; i < 1000000; i++)
{        
    id obj = [[NSNumber alloc] initWithInt:i];        
    id obj2 = [self returnParam:obj];
    NSLog(@"attempt %@", obj2);
}


However, if I put the content of the returnParam function in the loop as follows, everything works fine. The memory foot stamp stays the same size all long.

for(int i = 0; i < 1000000; i++)
{        
    id obj = [[NSNumber alloc] initWithInt:i];

    // Do whatever filtering needed. Break on invalid input.

    id obj2 = obj;

    NSLog(@"attempt %@", obj2);
}


I have eliminated the filtering part (so essentially the function only passes the object back to the caller), and the same situation persists.


Not understanding why this sequence does not decrease retain count as supposed, I tried all possible combination of __weak, __unsafe_unretained here and there, but none worked.

Can somebody explain why this (returning parameter object) doesn't work, and suggest me a solution of this issue?


P.S. BTW, it is not captured as a memory leak event in Instruments, but the situation as I consider is a obvious memory leak.


Solution

  • It's not a leak. It has to do with how -autorelease works. Expanding on what -returnParam: does:

    -(id) returnParam:(id) paramToReturn
    {
       // make sure the variable is in the scope for the entirety of the function
       id returnValue = nil;
       @try 
       {
           // when the parameter is passed in, ARC will automatically retain it.
           [paramToReturn retain];
           returnValue = [paramToReturn retain]; // strong assignment (a = b)
           return returnValue;
       }
       @finally
       {
           [paramToReturn release]; // release the value we retained when we passed in the parameter
           [returnValue autorelease]; // autorelease the return value so it is still valid after the function exits.
       }
    }
    

    Now, let's compare this to your other loop:

    for (int i = 0; i < 1000; i++)
    {
        id obj = [[NSNumber numberWithInt:i] retain]; // retain, because 'obj' is strong by default.
        id obj2 = [obj retain]; // retain, because 'obj2' is strong by default
    
        NSLog(@"attempt %@", obj2);
    
        [obj release]; // release because 'obj' is strong
        [obj2 release]; // release because 'obj2' is strong.
    }
    

    Thus, your variables aren't getting cleaned up until the next autorelasepool is popped, which usually occurs at the next NSRunLoop tick in an iPhone app, or possibly at the end of the @autoreleasepool in a console application.