Take the 2-minute tour ×

Obviously the following in the main method should lead to a leak:

NSMutableArray *strings = [[NSMutableArray alloc] init];
[strings addObject:@"Hello"];
[strings addObject:@"Howdy"];

return 0;

and the clang LLVM reports the leak. However, working through the Hillegass book, I tried analyzing the following code, which again does not release the NSMutableArray object:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSMutableArray *array = [[NSMutableArray alloc] init];
NSCalendarDate *now = [[NSCalendarDate alloc] init];

    LotteryEntry *newEntry = [[LotteryEntry alloc] init];
    NSCalendarDate *iweeksFromNow = [now dateByAddingYears:0
    [newEntry setEntryDate:iweeksFromNow];
    [array addObject:newEntry];
    [newEntry release];

[now release];

for (LotteryEntry *entry in array) {
    NSLog(@"%@", entry);

[pool drain];
return 0;

This time there was no leak reported. Am I missing something obvious?

share|improve this question
Wen you find a problem like this with the static analyzer and have such a nicely reduced example, please file a bug via bugreport.apple.com –  bbum Jan 1 '10 at 21:45
add comment

2 Answers

up vote 0 down vote accepted

I've asked this on the cfe-dev list and the answer is analysis in loops is halted at a given threshold (only 2 loop iterations are walked through).

If you change your exit condition (for (int i=0; i < 2; i++)) the static analyzer will signal the leak.

share|improve this answer
add comment

I don't think you are—the static analyzer's just missing it. array does end with a retain count of 1; if the line were

 NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];

then the enclosing autorelease pool would take care of it, but as it stands the array's definitely still leaking.

share|improve this answer
Or just [NSMutableArray array]. –  Adrian Jan 1 '10 at 20:06
add comment

Your Answer


