CoreGraphics,内存释放,返回值

时间:2012-09-18 05:25:25

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

当我从一个方法返回时,我似乎总是在使用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传递给它。如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

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

相关问题