memory, connection and request issue

Discussion in 'iOS Programming' started by MACloop, Apr 26, 2010.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    Hi,

    I have the following method and I think it is leaking. If I print out the retain for the request and connection objects, They are set to be 2. If I release them once again, additionally to the release I already do, the app crashes. As far as i can see 2 - (retain#1)- (retain#2) has to end up to be 0. On the the other hand, I do not understand why the retain of this object is 2 at this moment in the code. I just did create it by alloc/init. How can I do this differently? Any advice? I assume I did missunderstand something here...

    Thanks in advance!
    MACloop

    Code:
    #pragma mark connection help Methods
    - (void)requestWithUrls{
    	[receivedDatas removeAllObjects];
    	[requests removeAllObjects];
    			
    	for(NSInteger i=0; i< [urls count]; i++){
    		NSMutableData *aData = [[NSMutableData alloc] init];
    		[receivedDatas addObject: aData];
    		[aData release];
    		
    		NSURLRequest *request = [[NSURLRequest alloc]
    								 initWithURL: [NSURL URLWithString: [urls objectAtIndex:i]]
    								 cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
    								 timeoutInterval: 10];
    		NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    		[requests setObject: [NSNumber numberWithInt: i] forKey:[NSValue valueWithNonretainedObject:connection]];
    		
    		NSLog(@"connection retainCount:%d", [connection retainCount]);
    		NSLog(@"request retainCount:%d", [request retainCount]);
    		
    		[connection release];
    		[request release];
    	}
    }
    
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #2
    The request is likely being retained by NSURLConnection which would explain it having a retain count of 2. Its not leaking as far as I can tell.

    As for the connection, [NSValue valueWithNonRetainedObject:] would seem like it isn't retaining the connection but I don't really understand how this method works to say that with any certainty.
     

Share This Page