Why call autorelease for iVar definition in init method?

Posted by iFloh on Stack Overflow See other posts from Stack Overflow or by iFloh
Published on 2010-04-30T09:22:19Z Indexed on 2010/04/30 9:27 UTC
Read the original article Hit count: 279

Hi, I just familiarise myself with the CLLocationManager and found several sample class definitions that contain the following init method:

- (id) init {
    self = [super init];

    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
    }
    return self;
}

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

I don't understand why the iVar would be autoreleased. Does this not mean it is deallocated at the end of the init method?

I am also puzzled to see the same sample codes have the iVar release in the dealloc method.

Any thoughts? '

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cllocationmanager