Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARC and CFRelease?

I'm slightly confused. Everywhere I've read, suggest that when using ARC, you still need to release core foundation objects which makes sense, ARC doesn't manage them. However, I've got a method which uses some CF methods/objects which I used CFRelease on, but that then caused the app to crash. Uncommenting my CFReleases fixes the issue but then I'm assuming I've got a memory leak?

Could someone please explain which things need releasing and which don't, or anything else that's wrong with this code?

+ (NSString *) fileExtensionForMimeType:(NSString *)type {     CFStringRef mimeType = (__bridge CFStringRef)type;     CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);     CFStringRef extension = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);      NSString *ext = (__bridge NSString *)extension;      // CFRelease(mimeType);     // CFRelease(uti);     // CFRelease(extension);      return ext; } 

The three commented out CFRelease calls fix the issue as mentioned, but I know it's wrong. What should I be doing?

like image 467
PaReeOhNos Avatar asked Dec 28 '12 03:12

PaReeOhNos


People also ask

Does the arc publish grant guidelines on its website?

The ARC will no longer publish Grant Guidelines (formerly known as Funding Rules) on our website. All accountable authorities and officials must use GrantConnect to publish all Grant Opportunities, all Grant Guidelines and all addenda/alterations.

Where can I find all forecast opportunities for the arc?

All Forecast Opportunities for all schemes under the Discovery Program and Linkage Program are now available on GrantConnect. The ARC will no longer publish Grant Guidelines (formerly known as Funding Rules) on our website.

What is an arc book?

As mentioned, ARCs are unproofed, but mostly completed, electronic or bound physical copies of new books that have yet to be released to the general public for sale. ARCs are often missing author biographies, blurbs about the book, ISBN numbers, and the final sale price. They may or may not contain final photos and/or illustrations.

What is the complete arc of the circle?

An arc of the circle is the part of its circumference. Hence, we can say, the circumference is the complete arc of the circle. How to find the length of the arc?


2 Answers

You can't release mimeType because you don't own it. You didn't transfer ownership with the __bridge cast.

You should be releasing uti since you have created it.

You should also release extension since you created it as well, but that will likely cause issues with ext. Instead, transfer ownership to ext.

I'd suggest the following:

+ (NSString *) fileExtensionForMimeType:(NSString *)type {     CFStringRef mimeType = (__bridge CFStringRef)type;     CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);     CFStringRef extension = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);      NSString *ext = (__bridge_transfer NSString *)extension;      // CFRelease(mimeType); // not owned     if (uti) CFRelease(uti);     // CFRelease(extension); // ownership was transferred      return ext; } 
like image 178
rmaddy Avatar answered Sep 24 '22 09:09

rmaddy


Check out WWDC 2012 - Modern Objective-C which outlines new guidelines for Core Foundation objects and ARC. It's about 37:35 into that video. In short, Core Foundation functions with Copy or Create in the name create an object that has transferred ownership to your app, and your app is responsible for releasing it.

Anyway, if ownership has been transferred via a Core Foundation method with Copy or Create in the name, you can either release manually it with CFRelease when you're done with it, or, easier, you can transfer ownership to ARC and let it take care of it. Historically, to transfer ownership to ARC, we used __bridge_transfer, but they now recommend CFBridgingRelease (though the latter is just a macro for the former). And, obviously, if you have some Core Foundation object that you retrieved via some other mechanism other than a function with Copy or Create in the name, you should neither CFRelease it nor transfer ownership to ARC.

By way of illustration, this method accomplishes what you want:

+ (NSString *) fileExtensionForMimeType:(NSString *)type {      NSString *uti = CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,                                                                             (__bridge CFStringRef)type,                                                                             NULL));      return CFBridgingRelease(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uti,                                                              kUTTagClassFilenameExtension)); } 
like image 42
Rob Avatar answered Sep 20 '22 09:09

Rob