Re: mutableBytes Creates Autoreleased Objects
Re: mutableBytes Creates Autoreleased Objects
- Subject: Re: mutableBytes Creates Autoreleased Objects
- From: Charles Srstka <email@hidden>
- Date: Sat, 12 May 2012 12:18:04 -0500
On May 12, 2012, at 11:37 AM, Quincey Morris wrote:
> No, the pointer returned by 'mutableBytes' is an interior pointer. It *isn't* an object pointer. (Well, as an implementation detail, it may happen to point to an object's memory, but it often doesn't.)
>
> As soon as the NSMutableData's lifetime ends -- when it reaches its dealloc -- the 'mutableBytes' pointer becomes instantly invalid.
>
> Interestingly, this appears to mean that NSData/NSMutableData has become as dangerous in ARC as it is in GC. The compiler may shorten the lifetime of an object variable so that the interior pointer is catastrophically invalidated. This is documented in section 6.1 of the clang ARC spec:
>
> http://clang.llvm.org/docs/AutomaticReferenceCounting.html#optimization
Ugh, so you’re saying that ARC isn’t actually as deterministic as we’ve been led to believe?
> but it also give an answer:
>
> NSMutableData* data __attribute__ ((objc_precise_lifetime)) = …
>
> P.S. I think there's also another, better solution, but it involves adding a method to NSData/NSMutableData via a category:
>
> - (void*) interiorBytes __attribute__ ((objc_returns_inner_pointer)) {
> return self.bytes;
> }
>
> - (void*) mutableInteriorBytes __attribute__ ((objc_returns_inner_pointer)) {
> return self.mutableBytes;
> }
>
> and never using bytes/mutableBytes directly ever again. Perhaps one day bytes/mutableBytes will themselves be marked this way.
On the headers on my system, bytes and mutableBytes *are* marked that way.
Charles
_______________________________________________
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