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.
It’s not a leak. It has to do with how
-autoreleaseworks. Expanding on what-returnParam:does:Now, let’s compare this to your other loop:
Thus, your variables aren’t getting cleaned up until the next autorelasepool is popped, which usually occurs at the next
NSRunLooptick in an iPhone app, or possibly at the end of the@autoreleasepoolin a console application.