user945620
user945620

Reputation:

iOS: Release after autorelease

This can't be right... or maybe it is. I was looking at the source code for something called ShareKit, written by some guy named Nathan, and I noticed that he autoreleases a view that later, in dealloc, he releases.

Upvotes: 1

Views: 295

Answers (2)

mattjgalloway
mattjgalloway

Reputation: 34912

Not sure exactly why you think it's wrong. For example this is entirely valid code which exhibits the same sort of thing as you are seeing:

@interface SomeClass : NSObject

@property (nonatomic, retain) NSNumber *someNumber;

@end

@implementation SomeClass

@synthesize someNumber;

- (id)init {
    if ((self = [super init]])) {
        self.someNumber = [[[NSNumber alloc] initWithInt:5] autorelease]; // retain count = 1 (alloc) - 1 (on next drain) + 1 (retaining property) = 1
    }
}

- (void)dealloc {
    [someNumber release]; // retain count = 0
    [super dealloc];
}

@end

There's nothing wrong with a release happening after an autorelease.

Another bit of code that would be valid (although completely pointless!):

NSNumber *someNumber = [[NSNumber alloc] initWithInt:5]; // retain count = 1
[someNumber retain]; // retain count = 2
[someNumber autorelease]; // retain count = 2 (-1 on next drain) = 1
[someNumber release]; // retain count = 1 (-1 on next drain) = 0

Upvotes: 1

Denis
Denis

Reputation: 6413

No, there's no; Autorelease pool will just call a release for each instance it has in list on it's drain.

Upvotes: 0

Related Questions