代码之家  ›  专栏  ›  技术社区  ›  Dave Martorana

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

  •  11
  • Dave Martorana  · 技术社区  · 17 年前

    NSURLProtocol startLoading , [self client]

    <_NSCFURLProtocolBridge> {NSURLProtocol, CFURLProtocol}
    

    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];
    

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

    6 回复  |  直到 17 年前
        1
  •  4
  •   mekentosj    15 年前

    - (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;
    }
    
        2
  •  3
  •   bbum    17 年前
        3
  •  1
  •   Rob Napier    17 年前

    -retain CFRelease() CFRetain() .

    auto_zone_release

    p *($esp)
    

    po (id)(*($esp))
    
        4
  •  1
  •   Drew McCormack    15 年前

    我通过以下方式解决了这个问题 CFRetain CFRelease -下次再打电话给 startLoading

    -(void)startLoading 
    {
            if ( client ) CFRelease(client);
            client = [self client];
            CFRetain(client);
    

    -(void)finalize
    {
        if ( client ) CFRelease(client);
        [super finalize];
    }
    

    client NSURLProtocol 子类。

        6
  •  0
  •   psaghelyi    15 年前