[cfe-dev] Static analyser and objc init methods
Jean-Daniel Dupas
devlists at shadowlab.org
Thu Oct 30 02:08:56 PDT 2008
I'd just saw you already have an open bug for the false positive
(2972), I submited one for the missing leak report (2983).
Le 29 oct. 08 à 23:55, Ted Kremenek a écrit :
> Hi Jean-Daniel,
>
> This would be easy to add, although there may be a few tricky corner
> cases. If you don't mind, can you actually file a Bugzilla PR for
> this one (you can just copy the text of this email) so we can
> actually track the progress of implementing this feature?
>
> Ted
>
> On Oct 29, 2008, at 3:37 PM, Jean-Daniel Dupas wrote:
>
>> Hello,
>>
>> I think the memory leak analyser need a special semantic for objc
>> class initializer methods.
>>
>> Generaly, an initializer returns self, but in some case, it may
>> decide
>> to return something else (or throw an exception)
>> In these cases, 'self' must be release before the method returns else
>> it will leaks. (http://www.cocoabuilder.com/archive/message/cocoa/2008/2/11/198549
>> )
>>
>> Additionaly, if an init method returns a new object, this object
>> should be returns with a retain count to 1.
>>
>> for example:
>>
>>
>> - (id)initWithString:(NSString *)str {
>> if (!str) @throw [NSException
>> exceptionWithName:NSInvalidArgumentException];
>> if (self = [super init]) {
>> // do something with str
>> }
>> return self;
>> }
>>
>> Should detect a memory leak because when this method throws an
>> exception, it does not release self.
>>
>>
>> -------------------------------------------------------
>>
>> - (id)initWithVersion:(NSInteger)aVersion {
>> if (aVersion >= 10) {
>> return [self init];
>> } else {
>> [self release];
>> return [[SubClassWithOldBehavior alloc] init];
>> }
>> }
>>
>>
>> This method returns an object with retain count 1, but it is expected
>> and will not cause a memory leak (actually, clang report a leak
>> here).
>>
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
More information about the cfe-dev
mailing list