Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2012, 09:45 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 204
marciokoko is on a distinguished road
Question ARC & CF

I have written some code using the ABook framework, using ARC.

I am simply using a people picker and I used this:

Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    //1. get values and set labels...
    //1.a get the name
    NSString *fname = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    self.fullName.text = fName;
[self dismissModalViewControllerAnimated:YES];
    return NO;
}
So I understand Im casting a CFType (ABRecordCopyValue) and that causes a problem because of ARC.
At first I added this:

Code:
NSString *fname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
But I was told that doesn't do anything to the retain count, and that I had to use CFBridgingRelease to release that CFType. How exactly do I use it?
__________________
Mars
www.santiapps.com
www.gea-hn.com
mba-i4
marciokoko is offline   Reply With Quote
Old 01-27-2012, 10:15 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default Dealing with CF objects in ARC

Quote:
Originally Posted by marciokoko View Post
I have written some code using the ABook framework, using ARC.

I am simply using a people picker and I used this:

Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    //1. get values and set labels...
    //1.a get the name
    NSString *fname = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    self.fullName.text = fName;
[self dismissModalViewControllerAnimated:YES];
    return NO;
}
So I understand Im casting a CFType (ABRecordCopyValue) and that causes a problem because of ARC.
At first I added this:

Code:
NSString *fname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
But I was told that doesn't do anything to the retain count, and that I had to use CFBridgingRelease to release that CFType. How exactly do I use it?
Erica Sadun's new (and fantastic) "iOS 5 Developer's Cookbook" has an excellent section on ARC and using it with Core foundation classes. I was rather lost on using CF objects in ARC until I read about it there. I highly recommend buying her book. It is due to be released in 2 days on Amazon. Go order a copy now. Really. You won't regret it.

Anyway, in the meantime, here's the deal.

__bridge simply converts between CF and Objective C objects. It works in both directions. It does not adjust retain counts for you.

__bridge_transfer (or the macro CFBridgingRelease() ) converts a CF object to an NSObject. It is used for "toll-free bridged" CF objects. It lets you pass a CF object that you own to it's Objective C equivalent. The CF object is released in the process, and ARC takes ownership of the equivalent NSObject.

__bridge_retain (or the macro CFBridgingRetain() ) does the reverse of __bridge_transfer. It lets you convert an autoreleased NSObject to it's CF equivalent. The bridge CFRetains the resulting CF object, so you have to CFRelease it when you're done with it.

In your case, you are converting a CFString, which is toll-free bridged to NSString, to it's NSString equivalent. The ABRecordCopyValue method has copy in it's name, so it follows the "Create rule" for CF objects. You own it and need to release it. Thus, you should use this line:


Code:
NSString *fname = (NSString*) CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

after that line, fname holds a "strong" reference to the converted CFString, and will release it when it goes out of scope.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-27-2012, 07:37 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 204
marciokoko is on a distinguished road
Lightbulb ARC & CF Bridging

Ok could I ask why if I run the code with just __bridge, which should result in a leak because the CF is still around without being released, how come the leak doesn't show up in Leaks?

Is it because the Simulator doesn't have any ABook?



Quote:
Originally Posted by Duncan C View Post
Erica Sadun's new (and fantastic) "iOS 5 Developer's Cookbook" has an excellent section on ARC and using it with Core foundation classes. I was rather lost on using CF objects in ARC until I read about it there. I highly recommend buying her book. It is due to be released in 2 days on Amazon. Go order a copy now. Really. You won't regret it.

Anyway, in the meantime, here's the deal.

__bridge simply converts between CF and Objective C objects. It works in both directions. It does not adjust retain counts for you.

__bridge_transfer (or the macro CFBridgingRelease() ) converts a CF object to an NSObject. It is used for "toll-free bridged" CF objects. It lets you pass a CF object that you own to it's Objective C equivalent. The CF object is released in the process, and ARC takes ownership of the equivalent NSObject.

__bridge_retain (or the macro CFBridgingRetain() ) does the reverse of __bridge_transfer. It lets you convert an autoreleased NSObject to it's CF equivalent. The bridge CFRetains the resulting CF object, so you have to CFRelease it when you're done with it.

In your case, you are converting a CFString, which is toll-free bridged to NSString, to it's NSString equivalent. The ABRecordCopyValue method has copy in it's name, so it follows the "Create rule" for CF objects. You own it and need to release it. Thus, you should use this line:


Code:
NSString *fname = (NSString*) CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

after that line, fname holds a "strong" reference to the converted CFString, and will release it when it goes out of scope.
__________________
Mars
www.santiapps.com
www.gea-hn.com
mba-i4
marciokoko is offline   Reply With Quote
Old 01-28-2012, 11:27 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by marciokoko View Post
Ok could I ask why if I run the code with just __bridge, which should result in a leak because the CF is still around without being released, how come the leak doesn't show up in Leaks?

Is it because the Simulator doesn't have any ABook?
Dunno. Probably. Instruments isn't perfect, and the simulator is FAR from perfect.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 407
10 members and 397 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:57 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0