Iphone——对我的操作中的漏洞感到困惑
我的应用程序有一个名为ServerRequest的NSOperation类,用于处理网络任务。根据仪器,它正在疯狂地泄漏。此外,instruments表示构建请求的代码正在泄漏,即使这不是一个NSO操作。但我已经按照苹果的建议建立了一个自动释放池,所以我不明白会发生什么事。有人能帮忙吗 我的部分代码如下:Iphone——对我的操作中的漏洞感到困惑,iphone,memory-management,memory-leaks,nsoperation,Iphone,Memory Management,Memory Leaks,Nsoperation,我的应用程序有一个名为ServerRequest的NSOperation类,用于处理网络任务。根据仪器,它正在疯狂地泄漏。此外,instruments表示构建请求的代码正在泄漏,即使这不是一个NSO操作。但我已经按照苹果的建议建立了一个自动释放池,所以我不明白会发生什么事。有人能帮忙吗 我的部分代码如下: -(void) main { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; self.data = [NSURLC
-(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.var=nil setter方法来释放-dealoc解构器中的变量
您是否尝试过旧的尝试过且正确的[var release],var=nil方法?Hm.如果!self.error self.error=nil在我疲惫的眼睛看来似乎不对劲。。。对于self.response内容也是如此。这就是为什么我更喜欢if self.error!=无它更切中要害,也没有那么神奇。无论如何,使用属性设置程序来发布IVAR看起来都是不正确的。@KennyTM:Hm。我同意这一点;但只有在ivar已经为零的情况下才发布ivar有点奇怪……你说得对,这很奇怪。我改变了它,如果它是非零释放它。应用程序现在在这一行崩溃,即如果自我反应=无[自我反应释放];//在这里撞车。响应与@property nonatomic,retainYes合成。如果我释放它,程序仍然会崩溃,说我正在向一个解除分配的对象发送消息。但如果我不发布它,仪器就会出现漏洞。我会看看是否能找到讨论为什么这是个坏主意的博客帖子。也许知道我所指的帖子的人,如果他们先找到的话,也可以发帖子。