Is drawRect -— leaking memory

2019-07-16 23:42发布

问题:

I am currenty seeing a problem with memory leaks, it seems to come from this code:

- (void)drawRect:(CGRect)rect
{

    CGImageRef cgImage = CGBitmapContextCreateImage(offScreenBuffer);
    UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];

    CGImageRelease(cgImage);

    [uiImage drawInRect:self.bounds];
    [uiImage release];

}

this method is called from touches events ...

-(void)drawPoint:(UITouch *)touch {  

    currentLoc = [[PointLocation alloc] init];

    currentLoc.location = [touch locationInView:self];

    self.previousPoint = self.point;

    self.point = currentLoc;
    [self drawToBuffer];

    [currentLoc release];

}

and this is draw to buffer....

-(void)drawToBuffer {


    CGFloat color[4] = {R,G,B,A};

    if (self.previousPoint != nil) {
    CGContextSetRGBStrokeColor(offScreenBuffer, color[0],color[1],color[2],color[3]);

    CGContextBeginPath(offScreenBuffer);
    CGContextSetLineWidth(offScreenBuffer, lane);
    CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);

    CGContextMoveToPoint(offScreenBuffer, previousPoint.location.x, previousPoint.location.y);
    CGContextAddLineToPoint(offScreenBuffer, point.location.x, point.location.y);

    CGContextDrawPath(offScreenBuffer, kCGPathStroke);

}
 [self setNeedsDisplay];

}

this is really slow....:( And I don't know why...

Every single time I draw in the selected view, this is called, but every time I need to create an image and show it on the screen. Is there any possibility how to do this another way??

回答1:

The posted code inside drawRect should not be showing any leaks, as the images are properly released.



回答2:

Given this information the only possible leak that I can imagine could be related to offScreenBuffer.
If you show more code or the instruments traces it might help find the problem.

Also given the "SIZE" of the memory leak you can see if it is the image which if leaking or its something else and you are misreading the instruments reports.



回答3:

I currently solved this problem. It was a problem of getting saved to data memory one again a image of defined size. I need to create a drawing api from the beginning, it took some time, but it was better than updating unfunctional version. Thanks all for cooperation =)