Re: Are these the same and correct?
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