在此代码中释放CFStringRef的位置

时间:2012-01-27 14:42:21

标签: iphone ios memory-management

我有以下代码。我对保留/发布概念有点不确定。

for (int i = 0; i < ABMultiValueGetCount(urls)  ; i++) {
    CFStringRef url = ABMultiValueCopyValueAtIndex(urls, i);
    CFStringRef urlType = ABMultiValueCopyLabelAtIndex(urls, i);
    urlString  = (NSString *)url;
    urlTypeString = (NSString *)urlType;
    if ([urlTypeString hasPrefix:@"_"]) {
        NSString * urlTypeSubString = [urlTypeString substringWithRange:NSMakeRange(4, ([urlTypeString length]-8))];
        [mutableArray addObject:[NSString stringWithFormat:@"URL; %@: http://%@",urlTypeSubString,urlString]];
    }else{
        [mutableArray addObject:[NSString stringWithFormat:@"URL; %@: http://%@",urlTypeString,urlString]];
    }
}

我有两个问题。

  1. 我应该在哪里发布此代码中的'CFStringRef url'和'CFStringRef urlType'。

  2. 我应该发布'urlString'和'urlTypeString',它们是字符串类型。

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

在你的for循环结束时

for (int i = 0; i < ABMultiValueGetCount(urls)  ; i++) 
{
    CFStringRef url = ABMultiValueCopyValueAtIndex(urls, i);
    CFStringRef urlType = ABMultiValueCopyLabelAtIndex(urls, i);
    urlString  = (NSString *)url;
    urlTypeString = (NSString *)urlType;
    if ([urlTypeString hasPrefix:@"_"]) {
        NSString * urlTypeSubString = [urlTypeString substringWithRange:NSMakeRange(4, ([urlTypeString length]-8))];
        [mutableArray addObject:[NSString stringWithFormat:@"URL; %@: http://%@",urlTypeSubString,urlString]];
    }
    else
    {
        [mutableArray addObject:[NSString stringWithFormat:@"URL; %@: http://%@",urlTypeString,urlString]];
    }
    // Release here
    CFRelease(url);
    CFRelease(urlType);
}