artifacts / AFCache

AFCache is an HTTP cache for iOS and OSX seeking towards full RFC2616 compliance
Apache License 2.0
361 stars 43 forks source link

EXC_BAD_ACCESS on MAC OSX #14

Closed punayKimberly closed 9 years ago

punayKimberly commented 12 years ago

I've encountered EXC_BAD_ACCESS error

and when I used Zombienabled, I get a

*\ -[Not A Type retain]: message sent to deallocated instance 0x113d69ed0

I was kinda lost back there. I'd like to ask if you have encountered this kind of issue, also? Or I just need to do something with my implementation?

Great job, by the way!

punayKimberly commented 12 years ago

It seems to be that the NSURLCache response has a bit of a bug. I tried using the original code and the error didn't appear, however, when I used the subclass AFURLCache, the error showed up. After taking a lot of time of research on this issue, I decided to use a retain on the cachedResponse. Yes, it does give a memory leak, however, I don't see any other remedy for this, YET.

Hmm..well, if there's anyone there who has a better solution, please do share! Thanks!

sebbu commented 12 years ago

I don't know if you released the object to early or if AFCache did. But as you said, you could retain it ... and release it after you have used it. This way, you won't get a memory leak.

punayKimberly commented 12 years ago

I actually haven't touched anything from the AFCache original code when I encountered the crash. It seems that when using a subclass or NSURLCache (in using a subclass of NSURLCache to find out if WebKit can access certain cached resources). It seems that the NSURLCache releases cached "NSCachedURLResponse" objects too often. I get crashes later when these objects are released when they are already deleted.

Here's the code where the app crashes (AFURLCache):

-(NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
{
    NSURL* url = request.URL;
    AFCacheableItem* item = [[AFCache sharedInstance] cacheableItemFromCacheStore:url]; 

    if (item && item.cacheStatus == kCacheStatusFresh) {
    NSURLResponse* response = [[NSURLResponse alloc] initWithURL:item.url 
                                                        MIMEType:item.info.mimeType 
                                           expectedContentLength:[item.data length] textEncodingName:nil];      
    NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:item.data userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly];
    [response release];

    /*
     Done to avoid crash in mac osx 10.6 leopard (though this causes a memory leak)
     There's no other way for subclassed NSURLCache for now. This problem doesn't occur in the 
     original class - only in the subclass. I'm still looking for another way
     to work this around w/o causing a memory leak; however, for now, this is the only way I know
     that would fix the crash in the autoreleased urlResponse.
     */
    [cachedResponse retain];  // THIS IS THE RETAIN THAT I ADDED

    return [cachedResponse autorelease]; // CRASHING HERE

    } else {
    //NSLog(@"Cache miss for file: %@", [[AFCache sharedInstance] filenameForURL: url]);
    }

    NSCachedURLResponse *response = [super cachedResponseForRequest:request];
    return response;    
}

but if the NSURLCache is not subclassed, the error doesn't occur.

artifacts commented 9 years ago

AFCache is arc only now, it's unlikely that the problem still persists