Re: mutableBytes Creates Autoreleased Objects
Re: mutableBytes Creates Autoreleased Objects
- Subject: Re: mutableBytes Creates Autoreleased Objects
- From: Jens Alfke <email@hidden>
- Date: Sat, 12 May 2012 12:56:05 -0700
On May 12, 2012, at 12:31 PM, Quincey Morris wrote:
> I think the difference is that for UTF8String, there is an API contract that promises the result will be an object (and it has the lifetime behavior of any returned object that is returned with +0 retain semantics, as the documentation warns).
No; -[NSString UTF8String] returns a char*, not an object.
The difference is that -UTF8String has to allocate new memory to hold the result, because it's not in the same format as the internal string data (which is either UTF-16 or MacRoman and not null-terminated.) I believe internally it creates an autoreleased NSData and returns a pointer to its -bytes.
-mutableBytes doesn't (and shouldn't!) allocate anything; it just hands back a raw pointer to the NSData's bytes. But what it look like (as Ken said) is that the implementation of -mutableBytes calls [[self retain] autorelease] to avoid a situation where the caller releases the NSMutableData but still wants to use the bytes. The retain+autorelease ensures that the object and its bytes hang on until the inner autorelease pool is drained. In other words, I don't believe -mutableBytes allocates any data, but it does prolong the lifespan of its receiver.
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden