1

当我使用以下代码时,我被告知存在泄漏:

- (void)dealloc
{
    [connection release], connection = nil;
    [responseData release],responseData = nil;
    [cityCode release], cityCode = nil;
    [requestUrlString release], requestUrlString = nil;
    [returnDataDic release], returnDataDic = nil;

    [super dealloc];
}

- (id)initWithCityCode:(NSString *)aCityCode 
            requestURL:(NSString*)urlString 
          responseType:(SWEngineRequestType)theResponsetype 
                target:(id)theTarget 
                action:(SEL)theAction
{
    if ((self = [super init])) 
    {
        _isExecuting = NO;
    _isFinished = NO;
    target = theTarget;
    action = theAction;
    cityCode = [aCityCode retain];
        requestUrlString = [urlString copy];
        responseType = theResponsetype;
        returnDataDic = [[NSMutableDictionary alloc] initWithCapacity:1];
        if (cityCode) 
        {
             [returnDataDic setObject:cityCode forKey:SWEATHER_CITYCODE];
        }
        [returnDataDic setObject:[NSNumber numberWithInt:responseType]      forKey:SWEATHER_DOWNTYPE];
    }
    return self;
}


- (BOOL)isConcurrent
{
return YES;
}


- (void)finish
{

[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
_isExecuting = NO;
    _isFinished = YES;

[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];

    [connection release], connection = nil;
    [responseData release],responseData = nil;
    [cityCode release], cityCode = nil;
    [requestUrlString release], requestUrlString = nil;
    [returnDataDic release], returnDataDic = nil;
    done = YES;
}

- (BOOL)isExecuting
{
return _isExecuting;
}

- (BOOL)isFinished
{
return _isFinished;
}

- (void)main
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    done = NO;
    if ([self isCancelled]) 
    {
    [self willChangeValueForKey:@"isFinished"];
    _isFinished = YES;
    [self didChangeValueForKey:@"isFinished"];
        [pool release];
    return;
}

[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];

    NSURL * urlToDownLoad = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlToDownLoad cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) 
    {
    responseData = [[NSMutableData alloc] init];
        [connection start];
}
    else 
    {
    [self finish];
}

    if (connection != nil) 
    {
        do 
        {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        } 
        while (!done);
    }

    [pool release], pool = nil;
}

#pragma mark -
#pragma mark - NSURLConnectionDataDelegate methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [responseData appendData:data];
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    [returnDataDic setObject:@"error" forKey:...];
[target performSelectorOnMainThread:action withObject:returnDataDic waitUntilDone:NO];
    [self finish];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [returnDataDic setObject:responseData forKey:...];
    [target performSelectorOnMainThread:action withObject:returnDataDic waitUntilDone:NO];
    [self finish];
}
@end

仪器给了我一个泄漏:[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];为什么?谢谢!

我只想在一个操作中进行异步下载,但我使用了NSAutoreleasePool然后仪器在以下位置出现了泄漏:[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

4

2 回答 2

2

试着把你的

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

在自动释放池中。

于 2012-09-26T07:28:45.100 回答
0

Instruments 识别出哪些对象已泄漏?它分配在哪里?堆栈跟踪是什么?

当你运行一个运行循环时,所有的运行循环源和运行循环观察者都可能触发。因此,这几行代码隐藏了在运行循环运行时可能发生的几乎无限的事情。其中任何一个都可能有泄漏,或者仪器可能是错误的。

在内部循环中以默认模式运行运行循环通常是一个坏主意。目前尚不清楚您要使用该循环做什么,但通常您应该在私有运行循环模式下安排您自己的任何运行循环源,然后在该模式下运行运行循环。这样,您可以确定只有您自己的源代码才能运行,这通常是您想要的。

于 2012-04-04T09:07:19.800 回答