Re: mutableBytes Creates Autoreleased Objects
Re: mutableBytes Creates Autoreleased Objects
- Subject: Re: mutableBytes Creates Autoreleased Objects
- From: Greg Parker <email@hidden>
- Date: Wed, 16 May 2012 17:00:45 -0700
On May 12, 2012, at 1:55 PM, Dave Fernandes <email@hidden> wrote:
> So when a method is declared __attribute__ ((objc_returns_inner_pointer)), then LLVM tracks regular pointers like it would NSObject pointers to see when the owning object can be dealloced? Just want to make sure I understand.
In theory, the definition of objc_returns_inner_pointer allows ARC to track the returned pointer and release the owning object after the returned pointer is no longer used.
In the current implementation, ARC simply retains and autoreleases the owning object and makes no attempt to track the returned pointer.
You can suppress the extra retain/autorelease by using objc_precise_lifetime:
void *bytes;
{
NSMutableData *obj __attribute__((objc_precise_lifetime)) = ...;
// ARC preserves obj's strong reference until variable obj goes out of scope.
bytes = [obj mutableBytes];
// Because obj is marked precise-lifetime, ARC does not retain/autorelease the NSData object here.
*bytes++;
// bytes remains valid here without adding autorelease overhead
}
// bytes may be invalid here because obj is no longer in scope.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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