我应该释放 NSURL 吗?
我有以下代码:
+ (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,你是正确的。您不需要释放 loginURL。
但是我注意到代码中的其他地方确实过度释放了
logoutRequest
。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
.为什么要释放 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.