views:

42

answers:

1

Hello,

I'm trying to set a property like so -

-interface:

@property (readwrite, assign) CGColorRef otherBallColor;

-someMethod:

CGColorRef ballColor = [UIColor colorWithRed:255.0/256.0 green:165.0/256.0 blue:239.0/256.0 alpha:1.0].CGColor;  
[self setOtherBallColor:ballColor];

As soon as I try to access the value it crashes -

-someOtherMethod (drawRect in this case):

CGContextSetFillColorWithColor(context, otherBallColor);

But if in the "someMethod" I do -

CGColorRef ballColor = [UIColor blueColor].CGColor;

... it all works fine. Can anyone explain what's going on?

Many thanks (PS, quite new to Objective-C, tho not programming in general)

+1  A: 

You must retain returned CGColor, your [UIColor colorWith...] creates an auto-released instance, so, when it is out of scope (autoreleased I mean), corresponding CGColor is released as well.

I'd recommend you to use UIColor instead of CGColorRef if i is possible in this case.

Gobra
... and use CGColorRetain instead of `-retain`.
KennyTM
Franklyn Weber
Yes, or (what I personally prefer) use higher level object where it is possible. UI/NS classes look better, read easier and they are easier to manage in most cases
Gobra
CGColorRetain! Thanks - where do you find this stuff? I thought I was checking the documentation quite thoroughly, but I missed that. I did wonder though how I was supposed to retain something which wasn't an object like a UIColor...
Franklyn Weber