iossdkcaemitterlayercaemittercell

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


Here is my code -- Compiling on Xcode 6.3 for iOS8 and above:

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

I have been struggling with what to release!

Thanks!


Solution

  • The "Create Rule" says that if the Core Foundation call has "Create" or "Copy" in the name, you must release it (or transfer ownership to ARC, which isn't relevant here).

    The documentation for CGColorCreateCopy also makes this explicit and says:

    You are responsible for releasing this object using CGColorRelease.

    So you could remedy this by releasing the object returned by CGColorCreateCopy.

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

    Obviously, much easier is to just avoid CGColorCreateCopy altogether:

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