Skip to content

Instantly share code, notes, and snippets.

@0xced
Created August 10, 2012 09:37
Show Gist options
  • Save 0xced/3312967 to your computer and use it in GitHub Desktop.
Save 0xced/3312967 to your computer and use it in GitHub Desktop.
Yet another NSURLCache bug
/*
This crashes (usually after the first log, but not 100% reliable) on at least iOS 4.3, 5.0 and 5.1
(lldb) bt
* thread #1: tid = 0x1c03, 0x36d60e58 CoreFoundation`CFRetain + 20, stop reason = EXC_BAD_ACCESS (code=1, address=0xe0000000)
frame #0: 0x36d60e58 CoreFoundation`CFRetain + 20
frame #1: 0x33927248 CFNetwork`__CFURLCache::CopyResponseForRequest(_CFURLRequest const*, bool) + 116
frame #2: 0x339271cc CFNetwork`_ZL34__CFURLCacheCopyResponseForRequestPK11_CFURLCachePK13_CFURLRequestb + 48
frame #3: 0x33927e5c CFNetwork`CFURLCacheCopyResponseForRequest + 24
frame #4: 0x32a7487e Foundation`-[NSURLCache cachedResponseForRequest:] + 66
frame #5: 0x0006963c NSURLCacheCrash`main + 504 at main.m:28
*/
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
@autoreleasepool
{
NSURLCache *sharedURLCache = [NSURLCache sharedURLCache];
NSURL *dummyURL = [NSURL URLWithString:@"nsurlcache:ready"];
NSURLResponse *dummyResponse = [[NSURLResponse alloc] initWithURL:dummyURL MIMEType:nil expectedContentLength:0 textEncodingName:nil];
NSCachedURLResponse *dummyCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:dummyResponse data:[NSData data]];
NSURLRequest *dummyRequest = [NSURLRequest requestWithURL:dummyURL];
[sharedURLCache storeCachedResponse:dummyCachedResponse forRequest:dummyRequest];
NSCachedURLResponse *cachedResponse = nil;
while (!cachedResponse)
{
cachedResponse = [sharedURLCache cachedResponseForRequest:dummyRequest];
NSLog(@"cachedResponse: %@", cachedResponse);
}
return EXIT_SUCCESS;
}
}
@PrideChung
Copy link

I ran into same situation, the sharedURLCache was released somehow, it will raise a zombie message if you test it in instruments, not quite sure it's a bug or my fault, but I couldn't make it right.

@PrideChung
Copy link

Looks like this bug was fixed in iOS 6.0 and later, I've just tested it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment