【问题标题】:CoreGraphics, Memory Deallocation, Return ValuesCoreGraphics,内存释放,返回值
【发布时间】:2012-09-18 05:25:24
【问题描述】:

当我从一个方法返回时,我似乎总是在使用 CoreGraphics 管理内存时遇到很多麻烦。以下面的情况为例:

- (id) init
{
    CGMutablePathRef mutablePath = CGPathCreateMutable();

    //mutable path gets loaded with some hard data

    return [self initWithMutablePath:mutablePath];
}

- (id) initWithMutablePath:(CGMutablePathRef)mutablePath
{
    self = [super init];

    if (self) 
    {
        _mutablePath = (CGMutablePathRef) CGPathRetain(mutablePath);
    }

    return self;  
}

我从仪器收到这条消息:

Potential leak of an object allocated on line 38 and stored into 'mutablePath'

我确实希望 initWithMutablePath:(CGMutablePathRef)mutablePath 初始化程序保留 mutablePath,因为它可能会从需要它的其他地方调用。然而,我也希望 init 方法能够毫无意外地将 mutablePath 传递给它。如何实现?

【问题讨论】:

  • CGPathRetain 可能是原因.. CGPathCreateMutable 为您提供保留状态,为什么再次使用 CGPathRetain?
  • @LithuThiruvathira - 是的,我认为是的,但也需要从其他地方调用初始化程序,所以我需要保持这种方式。
  • 制作 CGMutablePathRef mutablePath = CGPathCreateMutable();在 if 循环中 if(mutablepath) 然后将其添加到 initWithMutablePath: 方法中,从 init 方法中删除 tht
  • 感谢@LithuThiruvathira,你知道我刚刚摆脱了其中一个初始化程序,现在它工作正常。除非想让它变得混乱,否则我试图做的似乎是不可能的。不过感谢您的 cmets。

标签: iphone ios cocoa-touch memory-management core-graphics


【解决方案1】:

似乎我想要做的事情是不可能的,所以我最终只是摆脱了 initWithMutablePath (这是基类的构造函数),我最终没有在基类中实现构造函数。我有继承它的子类实现初始化程序并加载路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多