Iphone-对我的NSOperation中的泄漏感到困惑 [英] Iphone -- Confused about leaks within my NSOperation
问题描述
我的应用程序具有一个名为ServerRequest的NSOperation类,该类可处理网络任务.根据仪器,它像疯了似的泄漏着.此外,仪器表示即使不是NSOperation,构建请求的代码也会泄漏.但是我在设置自动释放池方面遵循了苹果的建议,因此我不知道这可能是怎么回事.有人可以帮忙吗?
My app has an NSOperation class called ServerRequest that handles networking tasks. According to instruments, it is leaking like crazy. Additionally, instruments says the code that builds the request is leaking, even though that is not an NSOperation. But I have followed Apple's advice in terms of setting up an autorelease pool, so I don't understand what could be the matter. Can anyone help?
我的部分代码如下:
-(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方法(不会泄漏):
and the postResult method (which is not leaking):
-(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];
}
}
最后,取消分配:
- (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方法来释放-dealloc
解构函数中的变量.
Some recommend avoiding the self.var = nil
setter approach to releasing variables in the -dealloc
deconstructor.
您是否尝试过旧的,久经考验的[var release], var = nil
方法?
Have you tried the old tried-and-true [var release], var = nil
approach?
这篇关于Iphone-对我的NSOperation中的泄漏感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!