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?
You can’t release
mimeTypebecause you don’t own it. You didn’t transfer ownership with the__bridgecast.You should be releasing
utisince you have created it.You should also release
extensionsince you created it as well, but that will likely cause issues withext. Instead, transfer ownership toext.I’d suggest the following: