• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Accessor methods and (auto)release <Memory trail>
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Accessor methods and (auto)release <Memory trail>


  • Subject: RE: Accessor methods and (auto)release <Memory trail>
  • From: "Jonathan E. Jackel" <email@hidden>
  • Date: Wed, 31 Jul 2002 10:45:12 -0400

As usual, I am nodding my head in agreement with Ondra.

> >> - (NSString *) title {
> >> return [[title retain] autorelease];
> >> }
> >
> > Sorry, perhaps I am just plain dumb, but in what situation this would
> > bring any advantage to
> >
> > -(NSString*)title { return title; }
> >
> > ? I just can't see it: we just increment the r.c. (presumably to two,
> > since -- being in an instance variable -- it would be retained
> > already), to be decremented back in some future... regardless in what
> > thread this is done, what is it good for?
>
> The API contract for most Cocoa APIs is that the following is valid:
>
> {
> str = [someObj title];
> ... do other stuff ...
> ... access str ...
> }
>
> If "do other stuff" ends up releasing the original recipient object
> (someObj), or more subtly, directly or indirectly changing the title of
> someObj (so that the previous one is released), str will be invalid
> unless it's lifetime was extended in the calling scope with
> retain/autorelease.


So, to flesh out your example, you are concerned about this kind of code:

{
str = [someObj title];
if (someCondition)
[someObj setTitle:@"foo"];
NSLog (@"The string is %@", str];
}

If so, it seems to me that the proper technique is to substitute

str = [[[someObj title] retain] autorelease];

for the first line of code. Perhaps I have misunderstood the contract, but
it seems to me that it is not the accessor method's responsibility to
guarantee that the reference will remain valid after it has been returned.
Rather, it is the responsibility of the code that uses the reference to
retain it if there is a concern that it might be released anywhere else,
whether that is in an accessor method or otherwise.

Am I wrong?

Jonathan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Accessor methods and (auto)release <Memory trail>
      • From: Ali Ozer <email@hidden>
References: 
 >Re: Accessor methods and (auto)release <Memory trail> (From: Ali Ozer <email@hidden>)

  • Prev by Date: Re: Treating several controls as an array (...or something)
  • Next by Date: Glyphs for menus
  • Previous by thread: Re: Accessor methods and (auto)release <Memory trail>
  • Next by thread: Re: Accessor methods and (auto)release <Memory trail>
  • Index(es):
    • Date
    • Thread