有人能告诉我为什么会得到"物体的潜在泄漏"在CGColorCreateCopy?

时间:2015-04-15 22:14:39

标签: ios sdk caemitterlayer caemittercell

这是我的代码 - 针对iOS8及更高版本的Xcode 6.3进行编译:

//Create the emitter layer
CAEmitterLayer *emitter = [CAEmitterLayer layer];
emitter.emitterPosition = position;
emitter.emitterMode = kCAEmitterLayerOutline;
emitter.emitterShape = kCAEmitterLayerCircle;
emitter.renderMode = kCAEmitterLayerAdditive;
emitter.emitterSize = CGSizeMake(100 * multiplier, 0);

//Create the emitter cell
CAEmitterCell* particle = [CAEmitterCell emitterCell];
particle.emissionLongitude = M_PI;
particle.birthRate = multiplier * 1000.0;
particle.lifetime = multiplier;
particle.lifetimeRange = multiplier * 0.35;
particle.velocity = 180;
particle.velocityRange = 130;
particle.emissionRange = 1.1;
particle.scaleSpeed = 1.0; // was 0.3

particle.color = CGColorCreateCopy([UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.5].CGColor);
UIImage *theimage = [UIImage imageNamed:@"tspark"];
particle.contents = (__bridge id)(theimage.CGImage);
particle.name = @"particle";

emitter.emitterCells = @[particle];

我一直在努力发布什么!

谢谢!

1 个答案:

答案 0 :(得分:2)

"Create Rule"表示如果Core Foundation呼叫在名称中有“创建”或“复制”,则必须将其释放(或将所有权转移到ARC,这与此处无关)。

CGColorCreateCopy的文档也明确说明了这一点:

  

您有责任使用CGColorRelease发布此对象。

因此,您可以通过释放CGColorCreateCopy返回的对象来解决此问题。

CGColorRef colorRef = CGColorCreateCopy([UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.5].CGColor);
particle.color = colorRef;
CGColorRelease(colorRef);

显然,更容易完全避免CGColorCreateCopy

particle.color = [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.5].CGColor;