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