views:

51

answers:

3

Usually I write my dealloc to look like this:

- (void)dealloc {
    [coffeeList release];
    [super dealloc];
}

But today I typed (see below) and was a little puzzled why I got an error running the code through the CLANG static analyser. As I say I don't usually add self to iVars in dealloc, but was just a little curious when I spotted this as to what was going on.

- (void)dealloc {
    [[self coffeeList] release];
    [super dealloc];
}

gary.

+4  A: 

I'm just guessing that clang notices [self something] release (or [self.something release]) goes against the memory management conventions. An object returned from a method not having "new", "init", or "copy" in its name should be autoreleased, so releasing it again would appear to be an over-release error.

Brian
+1  A: 

Because that's really bad form, and goes against how you should be doing things. CSA doesn't just check for semantic errors, it also checks that you're doing things the way you should be. Where the way you should be, is the way things are done in the documentation.

jer
I am only new to objective-c but to my eye both [[self coffeeList] release]; and [coffeeList release]; would seem to be the same. Although it would seem that the syntax does confuse LLVM 1.5 CLANG.
fuzzygoat
They are not the same. The memory management rules tell you to release only objects you own. What's going on here, is `[self coffeeList]` owns `coffeeList`, not you. What it implements is basically this: `return [[coffeeList retain] autorelease];` which if you know your rules, means it owns coffeeList, and will be released at some arbitrary point in the future. You need to release the ivar itself *directly*.
jer
Thank you jer, just what I was after, I wanted to understand whats going on, and now I do. Much appreciated ...
fuzzygoat
A: 

Is coffeeList declared as a property (and set to retain)? Then you should probably do:

[self.coffeeList release];
christo16
[self.coffeeList release] and [[self coffeeList] release] are exactly the same call.
gcamp
it is yes .... @property(nonatomic, retain) NSArray *coffeeList;
fuzzygoat
Make it `self.coffeeList = nil;` instead. Make sure you first remove observers on `coffeeList` if there are any, otherwise they'd be called for an object that's going away.
Michal