Re: autorelease use...
Re: autorelease use...
- Subject: Re: autorelease use...
- From: "Tristan O'Tierney" <email@hidden>
- Date: Mon, 18 Jul 2005 10:41:50 -0400
you yourself shouldn't be using autorelease unless you're writing a method
within an object, in which the callee should manage the instance of the
object you just created.
in other worse, use release at all times, unless the time occurs where you
need to "pass the burden" of memory management to somewhere else. this
usually only occurs when you're writing some sort of framework, or helper
class, that has static constructors (such as [NSString stringWithString]) or
an accessor which for some reason or another creates a duplicate object,
rather than returning a reference. both of these cases are rather specific
and generally don't occur all that often. many people use autorelease in
accessors / mutators. this depends upon how you write them, and if they need
to be multithreaded etc. but in most cases you don't even need autorelease
in accessors and mutators.
On 7/18/05, Luc Vandal <email@hidden> wrote:
>
> Thanks! So I can play it safe and use autorelease everywhere I don't
> want to retain a variable's content?
>
> Luc
>
>
> On 18-Jul-05, at 10:20 AM, Tristan O'Tierney wrote:
>
> > autorelease is a handy way to return a value, and guarantee that it
> > will be
> > freed even if the context in which it's returning do does not itself
> > call an
> > explicit [object release].
> >
> > for example say you do:
> >
> > - ( void ) myFunction {
> > NSString *str = [NSString stringWithString: @"my string"];
> > }
> >
> > once you reach the end of myFunction, you are NOT guaranteed that the
> > contents of str will still exist, as it has been autoreleased and upon
> > the
> > next iteration of the event loop this object will be released /
> > dealloced.
> > however, say you want to pay attention to this variable and keep it for
> > later use, you would then do a retain:
> >
> > - ( void ) myFunction {
> > NSString *str = [NSString stringWithString: @"my string"];
> > [str retain];
> > }
> >
> > at this point the object will stick around past the length of this
> > function
> > (guaranteed). think of autorelease as "release later" or, "let my
> > parent
> > callee determine if i should be retained, not myself." this removes the
> > confusion out of "who should manage memory with helper functions. the
> > caller
> > or callee?"
> >
> > On 7/18/05, Luc Vandal <email@hidden> wrote:
> >>
> >> Hi!
> >>
> >> when should I use autorelease? Should I use it every time I use for
> >> example init.. or stringWithString?
> >>
> >> Luc
> >>
> >> _______________________________________________
> >> Do not post admin requests to the list. They will be ignored.
> >> Cocoa-dev mailing list (email@hidden)
> >> Help/Unsubscribe/Update your Subscription:
> >>
> >> This email sent to email@hidden
> >>
> >
> >
> >
> > --
> > Tristan O'Tierney
> > - Email: tristan [TA] otierney <DOT> net
> > - Homepage: http://www.otierney.net
> > _______________________________________________
> > Do not post admin requests to the list. They will be ignored.
> > Cocoa-dev mailing list (email@hidden)
> > Help/Unsubscribe/Update your Subscription:
> >
> > This email sent to email@hidden
> >
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
--
Tristan O'Tierney
- Email: tristan [TA] otierney <DOT> net
- Homepage: http://www.otierney.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden