簡體   English   中英

Iphone —對我的NSOperation中的泄漏感到困惑

[英]Iphone — Confused about leaks within my NSOperation

我的應用程序具有一個NSOperation類,稱為ServerRequest,用於處理網絡任務。 根據儀器,它像瘋了似的泄漏着。 此外,儀器表示即使不是NSOperation,構建請求的代碼也會泄漏。 但是我在設置自動釋放池方面遵循了蘋果的建議,因此我不知道這可能是怎么回事。 有人可以幫忙嗎?

我的部分代碼如下:

-(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];
 }
}

最后,解除分配:

- (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];
}

一些建議避免使用self.var = nil setter方法來釋放-dealloc解構-dealloc變量。

您是否嘗試過舊的可靠的[var release], var = nil方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM