When I use NSRunLoop Instruments gave a leak

Go To StackoverFlow.com

1

When I use the following code I was told there is a leak:

- (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

the instrument gave me a leak at: [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; Why? Thanks!

I just want to have a Asynchronous download at a operation but I use the NSAutoreleasePool then the instrument gave a leak at the:[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];.

2012-04-04 08:30
by Louis de Quan


0

What object did Instruments identify as having leaked? Where was it allocated? What was the stack trace?

When you run a run loop, all of the run loop sources and run loop observers may fire. So those few lines of code hide a near infinite set of possible things happening as the run loop runs. Any one of those may have a leak, or Instruments may be mistaken.

It is usually a bad idea to run the run loop in the default mode in an inner loop. It's not clear what you're trying to do with that loop, but generally you should schedule any of your own run loop sources in a private run loop mode and then run the run loop in that mode. That way, you're sure that only your own sources get run, which is usually what you want.

2012-04-04 09:07
by Ken Thomases
wo just want have Asynchronous download at a operation ,I use the runloop to make the operation to wait until response gave me a return. thanks to gave me a help - Louis de Quan 2012-04-04 09:13
So, use -[NSURLConnection initWithRequest:delegate:startImmediately:] with NO, so it isn't automatically scheduled in the default mode. Then use -scheduleInRunLoop:forMode: to schedule it in a private mode (just use an arbitrary name for the mode). Then run your run loop in just that mode - Ken Thomases 2012-04-04 15:08
I do as you saying but there is also a leak at :[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]], I don't know where I have a error ,but I still want to thank you - Louis de Quan 2012-04-05 05:37
Well, according to what you say, you're still running in the default mode. You should run in your specific mode. Anyway, there's definitely a possibility of a leak, but you need to provide the specifics I requested above: what object leaked? Where was it allocated? What was the stack trace - Ken Thomases 2012-04-05 12:58


2

Try putting your

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

in an autorelease pool.

2012-09-26 07:28
by Twenty Seven
Ads