views:

261

answers:

3

I have in effect a string utility that returns a URL. I know that when you do a [[NSString alloc] initWithFormat you have to manaully release the result string. But this case is a bit complicated and I'm not sure how to deal with it.

In this code snippet I will be calling the getChartURL method from another class:

http://pastie.org/588817

+1  A: 

Autorelease while solve your leaks. It can be very handy, but just don't overuse it. It seems reasonable to use in this case though.

+(NSString*) getBaseURL {
    …
    return [baseURL autorelease];
}
amrox
+1  A: 
 +(NSString*) getBaseURL {
        NSUserDefaults *userSettings = [NSUserDefaults standardUserDefaults]; 
        NSString* host = [userSettings stringForKey:@"host"];
        NSString* port = [userSettings stringForKey:@"port"];
        NSString* baseURL = [[NSString alloc] initWithFormat: @"http://%@:%@", host, port];
        return baseURL;
    }

Here you probably want the caller to retain the string if they wish, you are doing it for them here and it can cause leaks if not used properly, the way i w ould code this is by declaring autorelease for the baseURL so

  NSString* baseURL = [[[NSString alloc] initWithFormat: @"http://%@:%@", host, port]autorelease];
        return baseURL;
    }

Here

    +(NSString*) getChartURL:(int)width height:(int)height labels:(BOOL)labels time:(int)time monitorId:(NSString*)monitorId ruleInstanceId:(NSString*)ruleInstanceId {
            NSString* returnURL;
            if ([self isConfigured]){
             NSString* suffix = [[NSString alloc] initWithFormat: @"/Mobile/ChartServlet?width=%d&height=%d&time=%d&monitor_id=%@&rule_instance_id=%@", width, height, time, monitorId, ruleInstanceId];
             returnURL = [[self getBaseURL] stringByAppendingString:suffix];
            } else { 
             if (width == 320) {
              returnURL = @"http://Iphoneopt.bravehost.com/smallchart.png"; 
             } else {
              returnURL = [[NSString alloc] initWithFormat: @"http://Iphoneopt.bravehost.com/largechart%d.png", time];
              labels = FALSE;
             }
            }
            if (labels) {
             NSString* labelsSuffix = [[NSString alloc] initWithFormat: @"&labels=%d", labels];
             returnURL = [returnURL stringByAppendingString:labelsSuffix];
            }

            NSLog(@"returnURL=%@", returnURL);
            return returnURL;
        }

here you are not releasing any of the string you allocated, i suggest using [NSString stringWithFormat:] instead o f allocating them, but if you alloc them you must release them somewhere.

Daniel
+1  A: 

suffix and labelsSuffix both need to be released after you append them.

returnURL = [[NSString alloc] initWithFormat: @"http://Iphoneopt.bravehost.com/largechart%d.png", time];

could be changed to:

returnURL = [NSString stringWithFormat: @"http://Iphoneopt.bravehost.com/largechart%d.png", time];

You would probably also want to do the same with your return value for getBaseURL above it.

criscokid