我应该释放 NSURL 吗?

发布于 2024-09-04 15:03:46 字数 1843 浏览 5 评论 0原文

我有以下代码:

    + (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; 
}
}

我在带有仪器的模拟器上运行代码,它报告以下泄漏: 替代文本 http://img267.imageshack.us/img267/7651/instruments.png< /a> 指向以下行:

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

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

I have the following code:

    + (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; 
}
}

I run the code on the Simulator with Instruments and it reports the following leak:
alt text http://img267.imageshack.us/img267/7651/instruments.png
pointing to the following line:

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

I am pretty sure that I don't have to release NSURL (due to the convention that I need to release only the objects created with init or copy). Am I wrong? or is there something else wrong with the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2024-09-11 15:03:46

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

但是我注意到代码中的其他地方确实过度释放了 logoutRequest

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

From what I can tell you are correct. You should not need to release loginURL.

However I notice that elsewhere in the code you do overrelease logoutRequest.

// Note no retain of logoutRequest here as is done elsewhere
ASIFormDataRequest *logoutRequest = [ASIFormDataRequest requestWithURL:logoutURL];   
// ... 
[logoutRequest release];
画离情绘悲伤 2024-09-11 15:03:46

为什么要释放 logoutRequest?你没有保留它。

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

Why are you releasing logoutRequest? You didn't retain it.

I don't see any problem with your usage of NSURL, they are all autoreleased.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文