2010-02-25 39 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

我会看看我是否可以找到讨论为什么这是一个坏主意的博客文章。也许有人知道我所指的帖子,如果他们首先发现帖子,也可以发帖。 –