我的应用程序有一个名为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