gpt4 book ai didi

iphone - 使用 SDURLCache(NSURLCache 的子类)时的内存泄漏

转载 作者:搜寻专家 更新时间:2023-10-30 20:27:29 25 4
gpt4 key购买 nike

我正在使用 Olivier Poitrey's SDURLCache (github 链接)作为 NSURLCache 的替代方案,以在 iPhone 应用程序中启用磁盘缓存。

它工作得很好,但奇怪的是,当返回磁盘缓存对象时会泄漏 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

有没有人遇到过这个问题?关于解决方案的任何想法?让我知道是否应该发布更多代码示例。

干杯。

更新:Tweet from Olivier Poitrey ,代码作者

I'm planning on removing the NSURLCache inherency and handle the memory cache, it may fix the leak

最佳答案

本身不是答案,而是意识。 iOS 5.0 及更高版本上的 NSURLCache 现在默认缓存到闪存和 RAM。因此,如果您使用 SDURLCache 以获得磁盘缓存,并且目标是 5.0 及更高版本,则没有理由使用 SDURLCache。

关于iphone - 使用 SDURLCache(NSURLCache 的子类)时的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2992704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com