Memory management in Coregraphics (iOS)

 ̄綄美尐妖づ 提交于 2019-12-02 03:23:40

Your mutablePath leak, because you release the previous one in -drawRect: (and only if m_drawStep == DRAW). As -setNeedDisplay just marks the view as needing to be redrawn. -drawRect: is only send on the next drawing cycle, so several -touchesMoved:withEvent: may append between, drawing cycles. There is also some leak, that can happen when m_drawStep != DRAW.

So remove CGPathRelease(mutablePath) from -drawRect: method and release it in -touchesMoved:withEvent:

CGPathRef cgPath = self.currentPath.path.CGPath;
CGPathRelease(mutablePath);
mutablePath = CGPathCreateMutableCopy(cgPath);        

[self setNeedsDisplay];

Also release the CGLayerRef you create after setting it in -setCurrentDrawingLayer: because you retain it in -setCurrentDrawingLayer:.

if(currentDrawingLayer == nil)
{
    // ...
    CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL); // layer is created, refCount == 1
    // ...
    [self setCurrentDrawingLayer:layer]; // layer is retained by -setCurrentDrawingLayer:, refCount == 2
     CGLayerRelease(layer); // release layer, refCount == 1

     // without it the previous release layer refCount still == 2, so in -setCurrentDrawingLayer:
     // CGLayerRelease(currentDrawingLayer) decrement refCount to 1 and leak...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!