Re: Singletons cause static analyzer warning
Re: Singletons cause static analyzer warning
- Subject: Re: Singletons cause static analyzer warning
- From: Chris Suter <email@hidden>
- Date: Fri, 11 Sep 2009 09:24:03 +1000
Hi Sydney,
On Fri, Sep 11, 2009 at 5:49 AM, Sidney San Martín
<email@hidden> wrote:
If I implement a singleton as described in the docs
(<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32>),
Xcode 3.2's static analyzer throws a warning:
MyGizmoClass.m:19:13: warning: Potential leak of an object allocated on line 19
[[self alloc] init]; // assignment not done here
^
Is the analyzer behaving correctly here? How can I placate it?
It's a false positive. I personally don't like the singleton pattern described in the documentation. I think the hoops they jump through are unnecessary. My pattern is just this:
+ (MySingleton *)sharedInstance
{
static MySingleton *instance;
if (!instance)
instance = [[MySingleton alloc] init];
return instance;
}
And if I care about thread safety I'll add code for that. I've never made the mistake of allocating a separate instance so I don't care for the other cruft that the documentation has and I find my code much easier to understand.
The Static Analyser shouldn't warn with the case above.
Kind regards,
Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden