我应该发布NSURL吗?

时间:2010-06-08 19:45:53

标签: iphone memory-management memory-leaks instruments

我有以下代码:

    + (BOOL)updateStatus:(NSString *)status forUsername:(NSString *)username withPassword:(NSString *)password {

NSURL *loginURL = [NSURL URLWithString:@"XYZ"];
ASIFormDataRequest *loginRequest = [[ASIFormDataRequest requestWithURL:loginURL] retain]; 
[loginRequest setPostValue:@VALUE forKey:@"SOME_KEY"]; 
[loginRequest setPostValue:username forKey:@"username"]; 
[loginRequest setPostValue:password forKey:@"password"];  
[loginRequest startSynchronous]; 

int loginStatusCode = [loginRequest responseStatusCode];
[loginRequest release];

if (loginStatusCode == 200) { //if we were able to login
    NSURL *updateURL = [NSURL URLWithString:@"ABC"];
    ASIFormDataRequest *updateRequest = [[ASIFormDataRequest requestWithURL:updateURL] retain]; 
    [updateRequest setPostValue:@VALUE forKey:@"SOME_KEY"]; 
    [updateRequest setPostValue:VALUE   forKey:@"SOME_KEY"];
    [updateRequest setPostValue:@"VALUE" forKey:@"SOME_KEY"]; 
    [updateRequest startSynchronous]; 

    int statusCode = [updateRequest responseStatusCode];
    [updateRequest release];

    NSURL *logoutURL = [NSURL URLWithString:@"ABC"];
    ASIFormDataRequest *logoutRequest = [ASIFormDataRequest requestWithURL:logoutURL]; 
    [logoutRequest setPostValue:VALUE forKey:@"KEY"]; 
    [logoutRequest startSynchronous]; 

    [logoutRequest release];
    if (statusCode == 200) { /
        return YES;
    } else {
        return NO;
    }
} else {
    return NO; 
}
}

我使用Instruments在模拟器上运行代码并报告以下泄漏: alt text http://img267.imageshack.us/img267/7651/instruments.png 指向以下行:

NSURL *loginURL = [NSURL URLWithString:@"XYZ"];

我很确定我不必释放NSURL(由于我只需要释放使用init或copy创建的对象的约定)。我错了吗?或者代码还有其他问题吗?

2 个答案:

答案 0 :(得分:1)

据我所知,你说的是对的。您不需要释放loginURL。

但是我注意到你在代码的其他地方过度发布logoutRequest

// Note no retain of logoutRequest here as is done elsewhere
ASIFormDataRequest *logoutRequest = [ASIFormDataRequest requestWithURL:logoutURL];   
// ... 
[logoutRequest release];

答案 1 :(得分:0)

你为什么要发布logoutRequest?你没有保留它。

我认为您使用NSURL没有任何问题,它们都是自动释放的。