[cfe-dev] Static analyser and objc init methods

Ted Kremenek kremenek at apple.com
Wed Oct 29 15:55:15 PDT 2008


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