Iphone 在NSMutableURLRequest上使用release时出现问题
我有一个关于内存管理的问题。当我创建一个Iphone 在NSMutableURLRequest上使用release时出现问题,iphone,objective-c,ios4,ipad,Iphone,Objective C,Ios4,Ipad,我有一个关于内存管理的问题。当我创建一个 NSMutableURLRequest方法返回应用程序崩溃后,我将其释放 如果我在NSMutableURLRequest上删除带有release的行,应用程序就会工作。但它让内存泄漏 怎么了 代码如下: - (NSString *) callServerWhaitReturn { NSMutableURLRequest * theRequest = [ NSMutableURLRequest requestWithURL: [NSURL URL
NSMutableURLRequest
方法返回应用程序崩溃后,我将其释放
如果我在NSMutableURLRequest
上删除带有release的行,应用程序就会工作。但它让内存泄漏
怎么了
代码如下:
- (NSString *) callServerWhaitReturn {
NSMutableURLRequest * theRequest = [ NSMutableURLRequest requestWithURL: [NSURL URLWithString: self.internalUrl] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];
[theRequest setHTTPMethod: @"POST"];
[theRequest setHTTPBody:[[NSString stringWithFormat:@"p1=%@", self.parameters] dataUsingEncoding: NSASCIIStringEncoding]];
NSURLResponse * response;
NSError * error;
NSData * result = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &response error: &error];
NSString * toReturn = [[[NSString alloc] initWithData: result encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"%@", toReturn );
[theRequest release];
if (response) {
[response release];
}
if (result) {
[result release];
}
[toReturn autorelease];
return toReturn;
}
是一个自动释放的对象。您不能释放它。您必须分配对象并取得所有权,然后才应释放它。如果您想释放
它,那么分配
这样的对象
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: self.internalUrl] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];
是一个自动释放的对象。您不能释放它。您必须分配对象并取得所有权,然后才应释放它。如果您想释放
它,那么分配
这样的对象
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: self.internalUrl] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];
requestWithURL:cachePolicy:timeoutInterval:
返回自动删除的对象。如果您没有保留它,就不应该发布它
以下是。requestWithURL:cachePolicy:timeoutInterval:
返回自动删除的对象。如果您没有保留它,就不应该发布它
以下是。您无法释放请求,因为您没有分配它
因此,请从代码中删除以下语句
[theRequest release];
使用NSMutableURLRequest
的initWithURL
方法,您无法释放请求,因为您没有分配它
因此,请从代码中删除以下语句
[theRequest release];
使用NSMutableURLRequest
的initWithURL
方法尝试使用[[NSMutableURLRequest alloc]initWithURL…
您得到的错误是什么?嗨,Vaibhav Tekam。错误不明确。应用程序崩溃很简单。但您是对的……我没有使用“alloc”!这就是问题所在。我尝试在没有alloc的情况下释放对象。尝试使用[[NSMutableURLRequest alloc]initWithURL…
您得到的错误是什么?嗨,Vaibhav Tekam。错误不明确。简单的应用程序崩溃。但您是对的…我没有使用“alloc”!这就是问题所在。我尝试在不使用alloc的情况下释放对象。请注意,您的“result”变量也是如此,因此释放行也会导致崩溃。并且您会自动释放“toReturn”两次,这将是另一个崩溃器。请注意,您的“result”变量也是如此,因此释放行也会导致崩溃。您呢自动释放“toReturn”两次,这将是另一个崩溃。在7KV7时,你回答了大量@morgan!再次,在7KV7时,你回答了大量@morgan!再次,TkS7时,你回答了大量@morgan!再次,Tks