【问题标题】:Memory Leak Error内存泄漏错误
【发布时间】:2011-10-11 05:52:27
【问题描述】:

我是 iPhone 应用程序开发的新手,当我对我的代码运行泄漏分析器 (XCode->Product->Analyze) 时,我无法纠正此错误。它向我展示了某条线上的对象可能泄漏。

1) 方法返回一个具有 +1 保留计数的目标 c 对象(由于引用)

2) 在第 128 行分配的对象稍后在此执行路径中未引用,并且保留计数为 +1(对象泄漏)。responeData 保留在属性声明部分中

-(IBAction)registerButtonPressed:(id)sender
{

self.responseData = [NSMutableData data];

NSString *username = txtUsername.text;

NSString *jsonstring = [NSString stringWithFormat:@"http://demo.elgghub.com/apis/services/api/rest/json/?method=register&username=%@",username];


NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jsonstring]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[connection release];

self.responseData = nil;

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Error" 
                      message:@"Please check your network connection and relaunch the application" 
                      delegate:self 
                      cancelButtonTitle:@"Dismiss" 
                      otherButtonTitles:nil, nil];

[alert show];

[alert release];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{

[connection release];

NSString *responseStringReg = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;


NSDictionary *login =(NSDictionary*)[responseStringReg JSONValue] ;
[responseStringReg release];
NSNumber *status = [login objectForKey:@"status"];
NSString *statusString = [status stringValue];
NSString *message = [login objectForKey:@"message"];

}

-(void)dealloc
{

 [responseData release];

 [demoView release];

 [super dealloc];
}

【问题讨论】:

  • 第 128 行是哪种方法?

标签: iphone memory memory-leaks


【解决方案1】:

如果第 128 行是您创建 NSURLConnection 的位置,则会出现警告,因为分析器无法知道它将在委托上释放。无论如何,您可能应该存储对它的引用,也许在实例变量中。

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 2010-11-11
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多