Objective C Naming Convention for an object that owns itself

Posted by Ed Marty on Stack Overflow See other posts from Stack Overflow or by Ed Marty
Published on 2010-05-12T17:23:03Z Indexed on 2010/05/12 18:04 UTC
Read the original article Hit count: 192

Filed under:
|
|

With the latest releases of XCode that contain static analyzers, some of my objects are throwing getting analyzer issues reported. Specifically, I have an object that owns itself and is responsible for releasing itself, but should also be returned to the caller and possibly retained there manually.

If I have a method like + (Foo) newFoo the analyzer sees the word New and reports an issue in the caller saying that newFoo is expected to return an object with retain +1, and it isn't being released anywhere. If I name it + (Foo) getFoo the analyzer reports an issue in that method, saying there's a potential leak because it's not deallocated before returning.

My class basically looks like this:

+ (Foo *) newFoo {
    Foo *myFoo = [[[Foo new] retain] autorelease];
    [myFoo performSelectorInBackground:@selector(bar) withObject:nil];
    return myFoo;
}

- (void) bar {
    //Do something that might take awhile
    [self release];
}

The object owns itself and when its done, will release itself, but there's nowhere that it's being stored, so the static analyzer sees it as a leak somewhere. Is there some naming or coding convention to help?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about xcode