Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
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];
	}
}
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.