i have a global variable
UIColor *textColor;
I am update this variable by the code
textColor = [UIColor colorWithRed:fr green:fg blue:fb alpha:1.0];
then assigning this color to Label like this
myLabel.textColor = textColor;
It only work once, when i again call with updated values and assign label new values app crashes开发者_如何学JAVA...
textColor = [UIColor colorWithRed:fr green:fg blue:fb alpha:1.0];
myLabel.textColor = textColor;
First of all, you should almost never use global variables in Objective-C. They get very ugly as you get more code.
That being said, retain it after you create it to solve the crash, and release it before you assign something new to it. You're seeing the autorelease pool release the color for you since nothing owns it after your function exits.
I realize this is not an answer, but I think it gives the question a little more detail -- I'm sure other people have had this occur.
I had a similar thing when using colorWithRed:, which did not occur with, e.g. [UIColor redColor]
I don't know what the difference is between
UIColor* mycolor = [UIColor redColor]
and
UIColor* mycolor = [UIColor colorWithRed: 1.0 green:0.0 blue:0.0 alpha:1.0];but there definitely sure is a difference.
My call stack showed my UIDeviceRGBColor being thrown away, in a class that isn't, at that point, interacting with the device.
#0 0x91d95156 in __kill #1 0x91d95148 in kill$UNIX2003 #2 0x91e27899 in raise #3 0x91e3d9b8 in abort #4 0x91e2c160 in szone_error #5 0x91e2c25d in free_tiny_botch #6 0x00ccbc63 in _CFRelease #7 0x004231bc in -[UIDeviceRGBColor dealloc] #8 0x00007774 in -[RotoCircle setCircleLine:] at RotoCircle.m:16 #9 0x000068bd in -[DrawableView setFillPattern:oneColor:otherColor:forObjectList:] at DrawableView.m:328 #10 0x00006ba3 in -[DrawableView taps3] at DrawableView.m:360
精彩评论