我的應用程序有一個名爲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];
}
嗯。 '如果(!self.error)self.error = nil'似乎對我疲倦的眼睛看起來不正確......同樣對於'self.response'的東西。 – Dirk
這就是爲什麼我更喜歡'if(self.error!= nil)'......它更重要,魔法更少。 –
在任何情況下使用屬性設置器來釋放ivars只是看起來不正確。 – kennytm