Recently I was having a problem with NSURLConnection showing memory leaks in Instruments even though it was being correctly allocated and released. On investigation it seemed the problem may be lying with the cache policy, however I was using NSURLRequestReloadIgnoringLocalCacheData in the NSURLRequest to completely ignore any caching requirements and was confused as to why allocation problems would then be associated with caching.

I’m still not exactly sure what the problem is (according to this thread it may be due to having the NSURLConnection run in a method that’s detached from the main thread) however, forceably setting the capacity of the URL cache before initiating the connection using the URL request seems to solve the problem. For example:

[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSURLConnection *connection =
	[[NSURLConnection alloc] initWithRequest:request delegate:self];

Hey presto! No more NSURLConnection leaks.

Comment now!