Memory management in Coregraphics (iOS)

后端 未结 1 421
一整个雨季
一整个雨季 2021-01-25 09:53

I am working on a drawing application, I am using CGlayers for drawing, So I open my canvas for drawing on click of a button,

I am using UIBezierPath and then converting

1条回答
  •  梦毁少年i
    2021-01-25 10:32

    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...
    }
    

    0 讨论(0)
提交回复
热议问题