开发者

How else can I avoid leaking this Core Foundation object?

The following leaks:

CFStringRef labelName = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(aMultiRef, indexPath.row));
    cell.textLabel.text = (NSString *)labelName;
    CFRelease(lab开发者_StackOverflow社区elName);

Wondering if there a way to rewrite it so it doesn't leak without breaking out & assigning ABMultiValueCopyLabelAtIndex(aMultiRef, indexPath.row) to a CFStringRef that I then need to manually CFRelease 2 lines later? Of course, it's not a big deal to do just that...I'm just curious.

Edit: Would CFAutoRelease work? see my comment below


Because of Copy/Get semantics you are required to release anything that comes out of an API with Copy in it. ABMultiValueCopyLabelAtIndex meets that requirement, so unfortunately you'll need to acquire this reference and release it later on.


You can autorelease using Objective-C (so long as you have a pool in place). Just cast to id first. E.g. [(id)labelName autorelease] will work fine and it’s perfectly legal (because CFStringRef is and toll-free bridged with NSString). You can actually do this with any CoreFoundation based type, although I don’t believe Apple publicly document this so in theory, this could change.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜