• 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: Are these the same and correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Are these the same and correct?


  • Subject: Re: Are these the same and correct?
  • From: Uli Kusterer <email@hidden>
  • Date: Mon, 14 Nov 2005 22:15:23 +0100

Am 14.11.2005 um 20:02 schrieb Sherm Pendley:
On Nov 14, 2005, at 1:43 PM, Brian O'Brien wrote:

NSNumber *n = [[[NSNumber alloc] initWithShort:val] autorelease];
and
NSNumber *n = [NSNumber numberWithShort:val];

They're *very* similar; in neither case are you responsible for releasing n later.


But, as a matter of internal implementation details, there's no guarantee that n is autoreleased in the second case, although it quite often will be.

Just to illustrate that it's not just an academic distinction, here are two calls that are likely to bite you if you assume they're autoreleased:


-(id)objectForKey:(NSString*)key;
-(id)objectAtIndex:(int)index;

The reason is that in this case, all the guarantee you get is that the objects returned are not owned by you (because they're owned by the array or dictionary). In fact, if you empty the array or dictionary, the objects will get released (and may thus disappear if you didn't explicitly retain them anywhere else). So, this is one case where an item you get and which you don't have to release actually doesn't stay around as long as an autoreleased item in some cases.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de


_______________________________________________ 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
References: 
 >Are these the same and correct? (From: "Brian O'Brien" <email@hidden>)
 >Re: Are these the same and correct? (From: Sherm Pendley <email@hidden>)

  • Prev by Date: Re: Is there a self?
  • Next by Date: Re: Volunteers to test on 10.2
  • Previous by thread: Re: Are these the same and correct?
  • Next by thread: Scaling a Text View without confusing the Scroll View
  • Index(es):
    • Date
    • Thread