iphone:内存泄漏问题:消息发送到解除分配的实例,为什么?

时间:2010-08-18 23:39:37

标签: iphone objective-c iphone-sdk-3.0

这就是问题所在,今天我调试项目时遇到了不好的时间,控制台说:[CFString release]: message sent to deallocated instance 0x12345

我发现了问题,还有解决方案,但我不确定错误发生的原因。

-(BOOL) sendRequest:(NSString *) message {
 //xml -> data
 NSString *xml = [self toXML:message ];
 NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

 NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init];
 [request setURL: [NSURL URLWithString:url] ];
 [request setHTTPMethod:@"POST"];
 [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
 [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
 [request setHTTPBody:data];

    // some code NOT related to the connection... (UI stuff)

 //finally.. send the request
 NSURLResponse *theResponse;
 NSError *error;
 NSData *resp=[NSURLConnection sendSynchronousRequest: request returningResponse:&theResponse error:&error];

 //[data release];
 //[xml release]; <-- if i uncomment this; i got the memory issue 

 if ( resp == nil ){
  return NO;
 }
    // some code that updates the UI
 return YES; 
}

所以,我的问题是为什么要释放xml:[xml release],挑起可怕的内存泄漏?我想:由于我不再使用xml内容,所以发布它是一个很好的做法。

1 个答案:

答案 0 :(得分:2)

XML是一个传递参数。你不拥有它,因此不应该释放它。