iOS 核心基础 - 分析警告

问题描述 投票:0回答:1

我正在分析我的应用程序并收到以下警告。有人可以帮我解决这个问题吗?所有其他错误都消失了。

enter image description here

这是代码:

- (void)drawInContext:(CGContextRef)context
{
    if (!self.isPresentationLayer) {
        self.contentsScale = [[UIScreen mainScreen] init].scale;
    }

    CGMutablePathRef path = CGPathCreateMutable();
    self.mainPath = (CGMutablePathRef)CGPathCreateCopy(path);

   CGContextSaveGState(context);

    [self customDrawInContext:context];
    [self drawMainPathImageInContext:context];

    CGContextRestoreGState(context);

    CGPathRelease(path);

    self.isAllowedToAnimate = YES;
}

这是警告消息:

1.) 调用函数“CGPathCreateCopy”返回一个保留计数 +1 的 Core Foundation 对象

2.) 对象泄漏:分配的对象在此执行路径中稍后不会被引用,并且保留计数为+1

ios foundation core-foundation
1个回答
0
投票

我认为问题出在这里:

self.contentsScale = [[UIScreen mainScreen] init].scale;

首先,你不需要

init
,因为主屏幕是
UIScreen
类的静态属性。只要写:

self.contentsScale = [UIScreen mainScreen].scale;

接下来,我认为问题与您的属性存储类型有关。

当你写下:

- (void)drawInContext:(CGContextRef)context
{
    if (!self.isPresentationLayer) {
        self.contentsScale = [[UIScreen mainScreen] init].scale;
    }

    CGMutablePathRef path = CGPathCreateMutable();
    // path as +1 retainCount because of 'create'

    self.mainPath = (CGMutablePathRef)CGPathCreateCopy(path);
    // self.mainPath as +1 retainCount because of 'copy'
    // self.mainPath as again +1 retainCount if the property storage type is retain, copy or strong (I think the error is here)

    CGContextSaveGState(context);

    [self customDrawInContext:context];
    [self drawMainPathImageInContext:context];

    CGContextRestoreGState(context);

    CGPathRelease(path);
    // path as -1 retainCount and so is released.

    self.isAllowedToAnimate = YES;
}
© www.soinside.com 2019 - 2024. All rights reserved.