So I have this method that I see on an open source project, it just boggles my mind why this doesn’t leak memory when I try to run static analyzer. Reason why I think it should leak is because it’s allocating memory but never releasing it. It does release it everytime the method is called (because of the =nil). Can any one shed light for me?
- (BOOL)isValid(NSString *)name
{
// Using a set so access is faster
static NSMutableSet *exp = nil;
// Setup the set once with AB testing info
if (exp == nil) {
exp = [[NSMutableSet alloc] initWithCapacity:5];
}
if (exp != nil) {
return YES;
}
return NO;
}
the analyzer recognizes the
staticstorage qualifier, and understands that it does not go out of scope (until termination, of course).not so. that happens the first time the method is called — only once because it is
static.