如何避免GC环境中自定义NSURLProtocol中_NSCFURLProtocolBridge中的引用计数下溢

Dav*_*ana 11 garbage-collection memory-management objective-c underflow

基础是我有一个习惯NSURLProtocol.在startLoading,[self client]类型:

<_NSCFURLProtocolBridge> {NSURLProtocol, CFURLProtocol}
Run Code Online (Sandbox Code Playgroud)

问题是在垃圾收集环境中运行.因为我正在写一个屏幕保护程序,所以我不得不把它垃圾收集起来.但是,_NSCFURLProtocolBridge协议似乎总是抛出:

malloc: reference count underflow for (memory_id_here), break on auto_refcount_underflow_error to debug

调试控制台的示例转储是:

ScreenSaverEngine[1678:6807] client is <_NSCFURLProtocolBridge 0x20025ab00> {NSURLProtocol 0x200258ec0, CFURLProtocol 0x20029c400} ScreenSaverEngine(1678,0x102eda000) malloc: reference count underflow for 0x20025ab00, break on auto_refcount_underflow_error to debug.

您可以看到发生了下溢<_NSCFURLProtocolBridge 0x20025ab00>.

当我突破时auto_refcount_underflow_error,它似乎堆栈追溯到URLProtocolDidFinishLoading::

id client = [self client];
...
[client URLProtocolDidFinishLoading:self];
Run Code Online (Sandbox Code Playgroud)

这个问题似乎已经存在了一段时间,但在网上似乎没有任何答案:

http://lists.apple.com/archives/cocoa-dev/2008/May/msg01272.html http://www.cocoabuilder.com/archive/message/cocoa/2007/12/17/195056

该错误仅在垃圾收集环境中显示这些列出的错误.有关如何在不引起内存问题的情况下解决此问题的任何想法?我假设这可能与NSURLProtocol下面的CF类型不正确地发布有关?

小智 4

上次 WWDC 上我们向一位 webkit 工程师确认了这个 bug,他可以在代码中看到这个 bug,所以希望他们能够修复它。解决方法是在 initWithRequest 方法中 CFRetain 客户端。

- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client
{
    // work around for NSURLProtocol bug
    // note that this leaks!
    CFRetain(client);

    if (self = [super initWithRequest:request cachedResponse:cachedResponse client:client])
    {
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)