Static analyser and objc init methods


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. (

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.

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?


I'd just saw you already have an open bug for the false positive (2972), I submited one for the missing leak report (2983).

I don't think that is a correct requirement, at least not directly as stated.

It could be an application life long object with a possibly a MAX_INT
retain count. It could be an object with a retain count greater then 1
but with deferred releases (added to the current autorelease pool one
or more times).


You're right. It should not be exactly one.

Do you think the following assumption is right ?

Clang should considere init methods that does not return self just like it does for "+alloc, -create… or -copy…" method family.