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 CFRelease
s 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?
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.
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.
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.
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?
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; }
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With