有人能告诉我为什么我在 CGColorCreateCopy 上得到 "Potential leak of an object" 吗?

Can someone tell me why I get "Potential leak of an object" on CGColorCreateCopy?

这是我的代码——在 Xcode 6.3 上为 iOS8 及更高版本编译:

//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];

我一直在纠结要发布什么!

谢谢!

"Create Rule" 表示如果 Core Foundation 调用的名称中有 "Create" 或 "Copy",您必须释放它(或将所有权转移给 ARC,这无关紧要这里)。

CGColorCreateCopy 的文档也明确指出:

You are responsible for releasing this object using 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;