Iphone - 对我的NSOperation中的泄漏感到困惑

时间:2010-02-25 20:11:25

标签: iphone memory-management memory-leaks nsoperation

我的应用程序有一个名为ServerRequest的NSOperation类,用于处理网络任务。据乐器说,它像疯了一样泄漏。此外,仪器表示构建请求的代码正在泄漏,即使这不是NSOperation。但我在建立自动释放池方面遵循了Apple的建议,所以我不明白可能是什么问题。有人可以帮忙吗?

我的部分代码如下:

-(void) main {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 self.data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  // lots of leakage here
 [self postResult]; // leakage here too
 [pool release];
}

和postResult方法(没有泄漏):

-(void) postResult {
// error handling code here

 if (![self isCancelled])
 {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc]init];
  if (self.data!=nil)
   [resultDictionary setObject:self.data forKey:kDataKey];
  if (self.response!=nil)
   [resultDictionary setObject:self.response forKey:kResponseKey];
  if (self.error!=nil)
   [resultDictionary setObject:self.error forKey:kErrorKey];
  NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  NSNotification *notification = [NSNotification notificationWithName: kDownloadCompleteName object: self userInfo: resultDictionary];
  [resultDictionary release];
  [center postNotification: notification];

  [pool drain];
 }
}

最后,dealloc:

- (void) dealloc {
 self.request = nil;
 self.data = nil;
 self.mutableData = nil;
 if (!self.error)
  self.error = nil;
 if (!self.response)
  self.response = nil;
 [super dealloc];
}

1 个答案:

答案 0 :(得分:0)

有些人建议避免使用self.var = nil setter方法在-dealloc解构器中释放变量。

您是否尝试过经过验证的[var release], var = nil方法?

相关问题