discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about object initialization and autorelease


From: David Chisnall
Subject: Re: Question about object initialization and autorelease
Date: Sat, 31 Mar 2012 23:12:26 +0100

On 31 Mar 2012, at 23:07, Omar Campos wrote:

> Well, what I usually do is declare my properties with @synthesize, initialize 
> them by either calling [[alloc] init] or connecting them to visual objects in 
> XCode's Interface Builder. The book I read on this also taught me to call 
> -release for every property on the class's -dealloc method. Is this wrong?

That's probably sensible, although the recommended style is to assign nil to 
the property in the dealloc method in non-ARC mode.  In ARC mode, the compiler 
does this all for you.  When you access a property, the synthesised method is 
something like this:

- (id)property
{
        return [[property retain] autorelease];
}

This ensure that if you do something like:

id old = [obj property];
[obj setProperty: new];

The value of old is still valid.  Without the autorelease, the -setProperty: 
call will release the value and cause it to be deallocated.

> I think I'm also gonna take the time to read up on Obj-C memory management, 
> to better get a grasp on it :)

I can recommend a good book ;-)

David

-- Sent from my Apple II




reply via email to

[Prev in Thread] Current Thread [Next in Thread]