0

我的应用程序有一个名为 ServerRequest 的 NSOperation 类来处理网络任务。根据仪器,它正在疯狂地泄漏。此外,instruments 表示构建请求的代码正在泄漏,即使这不是 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];
}
4

1 回答 1

0

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

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

于 2010-02-25T22:02:21.257 回答