2010-02-25 77 views
0

我的應用程序有一個名爲ServerRequest的NSOperation類來處理網絡任務。根據文書,它正在瘋狂地泄漏。另外,工具表示構建請求的代碼泄漏,儘管這不是NSOperation。但是我已經按照蘋果的建議設置了自動釋放池,所以我不明白可能是什麼原因。誰能幫忙?Iphone - 我的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]; 
} 
} 

最後,的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

嗯。 '如果(!self.error)self.error = nil'似乎對我疲倦的眼睛看起來不正確......同樣對於'self.response'的東西。 – Dirk

+0

這就是爲什麼我更喜歡'if(self.error!= nil)'......它更重要,魔法更少。 –

+0

在任何情況下使用屬性設置器來釋放ivars只是看起來不正確。 – kennytm

回答

0

有人推薦避免使用self.var = nil setter方法釋放-dealloc deconst中的變量ructor。

您是否嘗試過以前嘗試過的方法[var release], var = nil

+0

是的。它仍然是這樣的,如果我釋放它,程序崩潰,說我正在向一個釋放對象發送消息。但是,如果我不釋放它,儀器會顯示泄漏。 –

+0

self.var = nil有什麼問題? – DenNukem

+0

我會看看我是否可以找到討論爲什麼這是一個壞主意的博客文章。也許有人知道我所指的帖子,如果他們首先發現帖子,也可以發帖。 –