Jean Paul
Jean Paul

Reputation: 2439

Where to release the CFStringRef in this code

I have the following piece of code. I am a bit unsure about retain/release concepts.

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

I have 2 questions.

  1. Where should I release 'CFStringRef url' and 'CFStringRef urlType' in this code.

  2. Should I release 'urlString' and 'urlTypeString', which are string types.

Upvotes: 1

Views: 718

Answers (2)

X Slash
X Slash

Reputation: 4131

At the end of your for loop

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

Upvotes: 2

Nyx0uf
Nyx0uf

Reputation: 4649

At the end of the for...

And take a look at this : http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.html

Upvotes: 3

Related Questions