【问题标题】:Objective C: Received Memory Warning on ARC目标 C:在 ARC 上收到内存警告
【发布时间】:2012-08-14 02:55:15
【问题描述】:

我的应用程序出现内存警告,然后它崩溃了。错误,我认为是在我的触摸绘图功能上,因为当我尝试使用 alloc 工具运行它时,当我继续在屏幕上移动手指时,内存开始增加。

这是我的代码。

    touchSwiped = YES;

    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = currentTouch;
    currentTouch = [touch locationInView:self.view];

    CGPoint mid1 = midPoint(previousPoint2, previousPoint1); 
    CGPoint mid2 = midPoint(currentTouch, previousPoint1);

    UIGraphicsBeginImageContext(CGSizeMake(480 , 320));
    [drawView.image drawInRect:CGRectMake(0, 0, 480,320)];  

    CGContextRef context = UIGraphicsGetCurrentContext();    

    CGContextSetLineCap(context,kCGLineCapRound);
    CGContextSetLineWidth(context, inkWidth);
    CGContextSetBlendMode(context, blendMode);
    CGContextSetRGBStrokeColor(context,redColor, greenColor, blueColor, 1);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGContextStrokePath(context);

    drawView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsGetCurrentContext();

【问题讨论】:

  • 您为每次触摸/拖动创建了很多东西,即使使用 ARC,我相信您必须释放 CGContextRef 和其他一些东西。我不完全确定,因此这是一个评论。祝你好运!
  • 你说对了一半,@ohr。 Core Graphics 和 Core Foundation “对象”不是由 ARC 管理的内存,但是您从 UIGraphicsGetCurrentContext() 获得的 CGContextRef 不属于调用者(该函数不属于 have copy or create in its name),并且没有被释放。
  • @ohr 成功了!!谢谢!

标签: objective-c ios memory automatic-ref-counting


【解决方案1】:

在使用UIGraphicsBeginImageContext时,请务必在使用完毕后将其与UIGraphicsEndImageContext平衡,否则将无法整齐地清理。按照您的方法运行的代码仍然会认为当前的 UI 上下文是您手动发布的,可能会带来灾难性的后果。

【讨论】:

    【解决方案2】:

    我按照 Ohr 在他的评论中所说的做了,这对我有用。

    我像这样CGContextRelease(context);发布了CGContextRef

    【讨论】:

    • @Jesper 的回答是正确的。你不应该释放你没有保留的对象
    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2012-07-05
    • 2014-12-24
    • 1970-01-01
    • 2012-07-10
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多