0

我是 iPhone 开发的新手。我的问题是关于内存管理这是我的代码:

- (void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent {

urlAddress = [self encodeStringForURL:urlAddress];
theRequest = [[[NSMutableURLRequest alloc] init]autorelease];
[theRequest setURL:[NSURL URLWithString:urlAddress]];
if([postContent length] > 0) {
    [theRequest setHTTPMethod:@"POST"];
} else {
    [theRequest setHTTPMethod:@"GET"];
}



NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[theRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postLength = [NSString stringWithFormat:@"%d", [theBodyData length]];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:theBodyData];    

[NSThread detachNewThreadSelector:@selector(sendSyncRequest) toTarget:self withObject:nil];

}

我在这些方面得到了泄漏:

theRequest = [[[NSMutableURLRequest alloc] init]autorelease];
[theRequest setURL:[NSURL URLWithString:urlAddress]];
NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[theRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

如果有人可以帮助,那么请帮助我。感谢您。

4

1 回答 1

0

我猜你的泄漏是由这条线造成的:

urlAddress = [self encodeStringForURL:urlAddress];

我需要查看您的 encodeStringForURL 代码,但我猜您是否在没有自动释放的情况下将其传回。此外,我不会将该方法的结果分配给您的参数变量,然后您将失去对其的引用。你应该这样做:

NSString *encodedUrlAddress = [self encodeStringForURL:urlAddress];

然后在调用 URLWithString 时使用 encodedUrlAddress。

于 2012-05-22T06:11:33.867 回答