iOS CATiledLayer crash

↘锁芯ラ 提交于 2019-12-03 17:14:51
steipete

You need to set CALayer's delegate to nil, then remove it from the superview. This stops rendering, afterwards you can safely dealloc.

- (void)stopTiledRenderingAndRemoveFromSuperlayer; {
    ((CATiledLayer *)[self layer]).delegate = nil;    
    [self removeFromSuperview];
    [self.layer removeFromSuperlayer];
}

Also, make sure to call this from the main thread, or else horrible bugs will await you.

I haven't looked at the disassembly to see, but we are using a slightly different solution. Setting the CATiledLayer.content property to nil blocks and forces all queued render blocks to complete. That can safely be kicked off to a background thread, then releasing the UIView can be kicked back to the main thread to let the view and layer dealloc.

Here's one example of a UIViewController dealloc implementation that will keep your CATiledLayer owning view alive long enough to safely stop rendering, without blocking the main thread.

- (void)dealloc
{
    // This works around a bug where the CATiledLayer background drawing 
    // delegate may still have dispatched blocks awaiting rendering after
    // the view hierarchy is dead, causing a message to a zombie object.
    // We'll hold on to tiledView, flush the dispatch queue, 
    // then let go of fastViewer.
    MyTiledView *tiledView = self.tiledView;
    if(tiledView) {
        dispatch_background(^{
            // This blocks while CATiledLayer flushes out its queued render blocks.
            tiledView.layer.contents = nil;

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // Make sure tiledView survives until now.
                tiledView.layer.delegate = nil;
            });
        });
    }
}

This is a guess, but some of Apple's frameworks/classes (StoreKit, CATiledLayer, UIGestureRecognizer) claim to have @property (weak) id delegate implementations but clearly do not properly handle the weak delegate. Looking at some disassembly and they are doing decidedly race-bound if != nil checks then touching the weak property directly. The proper way is to declare a __strong Type *delegate = self.delegate, which will either succeed and give you a strong reference guaranteed to survive, or be nil, but it certainly won't give you a reference to a zombie object (my guess is that framework code has not been upgraded to ARC).

Under the hood, CATiledLayer creates a dispatch queue to do the background rendering and appears to either touch the delegate property in an unsafe way or it obtains a local reference but without making it a strong one. Either way, the dispatched render blocks will happily message a zombie object if the delegate gets deallocated. Just clearing the delegate is insufficient - it will reduce the number of crashes but does not safely eliminate them.

Setting the content = nil does a dispatch_wait and blocks until all the existing queued render blocks are finished. We bounce back to the main thread to make sure the dealloc is safe.

If anyone has suggestions for improvement please let me know.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!