ARC and CFRelease? ARC and CFRelease? ios ios

ARC and CFRelease?


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));}


In generally speaking, I think you should try to comment the first CFRelease(mimeType) line, and uncomment the followed two lines: CFRelease(uti) and CFRelease(extension). You cast a toll-free bridge to input NSString and ARC will handle the release. But the uti and extension is created/copied as CFString. ARC will not know how to handle them (remember ARC is a compiler feature for NSObject), so you need to CF release them.


matomo