2010-06-07 1 views
3

NSURLCache 대신 Olivier Poitrey's SDURLCache (github 링크)을 사용하여 iPhone 앱에서 디스크 캐시를 사용하도록 설정합니다.SDURLCache (NSURLCache의 하위 클래스)를 사용할 때의 메모리 누수

아주 잘 작동하지만 디스크 캐쉬 된 객체가 반환되면 (객체가 메모리에서 반환되거나 객체가 없을 때 누수가 없음) 기이하게도 NSHTTPURLResponseInternal이 누출됩니다. 다음 코드는 SDURLCache 디스크에서 데이터를 읽는 방법을 보여줍니다

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request 
{ 
    NSCachedURLResponse *memoryResponse = [super cachedResponseForRequest:request]; 
    if (memoryResponse) 
    { 
     return memoryResponse; 
    } 

    NSString *cacheKey = [SDURLCache cacheKeyForURL:request.URL]; 

    // NOTE: We don't handle expiration here as even staled cache data is necessary for NSURLConnection to handle cache revalidation. 
    //  Staled cache data is also needed for cachePolicies which force the use of the cache. 
    NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kSDURLCacheInfoAccessesKey]; 
    if ([accesses objectForKey:cacheKey]) // OPTI: Check for cache-hit in a in-memory dictionnary before to hit the FS 
    { 
     NSCachedURLResponse *diskResponse = [NSKeyedUnarchiver unarchiveObjectWithFile:[diskCachePath stringByAppendingPathComponent:cacheKey]]; 
     if (diskResponse) 
     { 
      // OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary 
      //  on disk now in order to save IO and time 
      [accesses setObject:[NSDate date] forKey:cacheKey]; 
      diskCacheInfoDirty = YES; 

      // OPTI: Store the response to memory cache for potential future requests 
      [super storeCachedResponse:diskResponse forRequest:request]; 
      return diskResponse; 
     } 
    } 

    return nil; 
} 

NSHTTPURLResponseInternal 누출의 각각에 대한 스택 추적은 SDURLCache 코드에 대한 두 개의 참조가 포함되어 있습니다.

첫 번째 점은 [accesses setObject:[NSDate date] forKey:cacheKey];입니다. 두 번째 및 최신 점은 다음과 같습니다.

@implementation NSCachedURLResponse(NSCoder) 

- (id)initWithCoder:(NSCoder *)coder 
{ 
    return [self initWithResponse:[coder decodeObjectForKey:@"response"] 
          data:[coder decodeDataObject] 
         userInfo:[coder decodeObjectForKey:@"userInfo"] 
        storagePolicy:[coder decodeIntForKey:@"storagePolicy"]]; 
} 

@end 

이 문제가 발생한 사람은 누구입니까? 솔루션에 대한 생각? 더 많은 코드 샘플을 게시해야하는지 알려주십시오.

건배.

UPDATE : Tweet from Olivier Poitrey, 나는 NSURLCache의 고유을 제거하고 메모리 캐시를 처리 계획하고있어

코드의 저자, ​​그것은 누출

답변

1

하지에게 답을 해결할 수 있습니다 그 자체가 아니라 오히려 인식. iOS 5.0 이상의 NSURLCache는 기본적으로 플래시와 RAM에 모두 캐시됩니다. 따라서, 디스크 캐싱을 얻기 위해 SDURLCache를 사용하고 5.0 이상을 목표로한다면 SDURLCache를 사용할 이유가 없습니다.