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 22:54:22 +0100

On 31 Mar 2012, at 22:39, Omar Campos wrote:

> > Also, coming from a iOS background,
> 
> If you come from iOS, is there a reason you choose not to use ARC?
> 
> 
> What exactly is ARC? I haven't heard the term before. 

ARC is Automatic Reference Counting.  It means that the compiler inserts the 
retain / release / autorelease messages for you automatically, and the 
optimiser elides them when it can prove that they are redundant.

> >The correct time to send an -autorelease message is when you want a 
> >non-owning reference that is guaranteed to persist for the >duration of 
> >scope of a local variable and you have a religious of philosophical aversion 
> >to ARC.
> 
> I did not understand this part. What exactly is a non-owning reference?

Objective-C pointer variables hold either owning or non-owning references.  An 
owning reference is one that contributes to the retain count, a non-owning 
reference does not.  

> In fact, why bother using -autorelease at all?

Autorelease defers a release message.  It is most often used for passing 
pointers to temporary variables up the stack.  If you are returning a pointer, 
you typically return a non-owning reference so that it can be stored in an 
on-stack variable without the caller needing to explicitly release it.  

> Doesn't the framework automatically create an autorelease pool when 
> NSApplicationMain() is called?

Yes.  An autorelease pool is created at the start and destroyed at the end of 
every run loop.  Sending an -autorelease message to an object means that it 
will be sent a -release message when the autorelease pool is destroyed.

> Sorry if I am sound like a total newbie ('cause I actually am!). As I said, 
> my Obj-C experience is with the iOS frameworks, particularly Cocoa Touch. 
> And, in my year and two months experience, I haven't had the need to use 
> -autorelease (or maybe there was, and my apps are leaking memory!).

If you have never used autorelease, your code is almost certainly either 
trivial or wrong.  That said, if you have used synthesised properties, then you 
have probably used autorelease without even being aware of it.

David

-- Sent from my Difference Engine






reply via email to

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