ARC and CFRelease?

2019-02-01 05:56发布

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?

3条回答
放我归山
2楼-- · 2019-02-01 06:02

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;
}
查看更多
老娘就宠你
3楼-- · 2019-02-01 06:21

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));
}
查看更多
叼着烟拽天下
4楼-- · 2019-02-01 06:23

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.

查看更多
登录 后发表回答