Re: Arcs and Bridges
Re: Arcs and Bridges
- Subject: Re: Arcs and Bridges
- From: Quincey Morris <email@hidden>
- Date: Sat, 15 Oct 2011 23:42:48 -0700
On Oct 15, 2011, at 23:16 , Gerriet M. Denkmann wrote:
> Formerly I had:
>
> CFStringEncoding encoding = ...
> CFStringRef axa = CFStringGetNameOfEncoding ( encoding );
> NSString *encodingName = (NSString *)axa;
> NSDictionary *d = [ [ NSDictionary alloc ] initWithObjectsAndKeys:
> encodingName, kEncodingName,
> nil
> ];
> // from here on encodingName is no more used (and on course also not released).
>
>
> Xcode 4.2 tells me that "NSString *encodingName = (NSString *)axa" is no good.
> And the "Transitioning to ARC Release Notes" did fail to enlighten me.
Try http://clang.llvm.org/docs/AutomaticReferenceCounting.html#objects.operands.casts, section 3.2.4.
> So, should I use:
> NSString *encodingName = (__bridge NSString *)axa;
> or:
> NSString *encodingName = (__bridge_transfer NSString *)axa;
Use '(__bridge NSString *)'. All you're doing is bridging from non-retainable to retainable pointer types, but you're not changing ownership.
> And what about this scenario:
> CFStringRef axb =CFStringCreateCopy(...);
> NSString *myString = (NSString *)axb;
> // do something with myString and finally do:
> [ myString release ];
>
> which bridge thing has to be used here?
Use '(__bridge_transfer NSString *)'. You're transferring ownership via 'axb' to ownership via 'myString', if that makes any sense.
ARC uses horribly ambiguous terminology here:
1. CFStringCreateCopy has +1 return semantics, so it's "transferring" ownership of the string object to "you". This relates to the retain count on the object.
2. The "__bridge" part of the keyword, according to section 3.2.4 "transfers" the object into ARC control. There's no *ownership* transfer implied by that part of the keyword.
3. The "_transfer" part of the keyword … well, I can't find any good language to describe it. It kinda "transfers" control of the ownership into ARC -- it tells ARC to release the object at the end of the relevant scope, thus doing the work of the explicit 'release' you'll have to delete.
I guess we'll have to grin and bear this sort of thing from here on in.
_______________________________________________
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