David Skrundz
David Skrundz

Reputation: 13347

1 out of 4 colors return errors

I have 4 Colors defined.

@interface Global : NSObject {

    UIColor *_EnemyColor;
    UIColor *_EnemyColor2;
    UIColor *_TeamColor;
    UIColor *_TeamColor2;

}

@property (nonatomic, retain) UIColor *EnemyColor;
@property (nonatomic, retain) UIColor *EnemyColor2;
@property (nonatomic, retain) UIColor *TeamColor;
@property (nonatomic, retain) UIColor *TeamColor2;

@end

and

@synthesize EnemyColor = _EnemyColor;
@synthesize EnemyColor2 = _EnemyColor2;
@synthesize TeamColor = _TeamColor;
@synthesize TeamColor2 = _TeamColor2;

Then in the init method, I try setting some colors to the variables.

- (id)init {
if (self = [super init]) {
    //*******************************************************************
    _TeamColor = [UIColor colorWithRed:0.70196078 green:0.70196078 blue:0.70196078 alpha:1.0];
    //Everything works, if this line is commented out
    _TeamColor2 = [UIColor colorWithRed:0.82352941 green:0.81960784 blue:0.83921569 alpha:1.0];
    switch (arc4random() % 4) {
        case 0:
            _EnemyColor = [UIColor colorWithRed:0.50196078 green:0.47843137 blue:0.41568627 alpha:1];
            _EnemyColor2 = [UIColor colorWithRed:0.63529412 green:0.57647059 blue:0.44705882 alpha:1];
            break;
        case 1:
            _EnemyColor = [UIColor colorWithRed:0.72156863 green:0.59607843 blue:0.37254902 alpha:1];
            _EnemyColor2 = [UIColor colorWithRed:0.81568627 green:0.73333333 blue:0.51764706 alpha:1];
            break;
        case 2:
            _EnemyColor = [UIColor colorWithRed:0.75686275 green:0.47843137 blue:0.23529412 alpha:1];
            _EnemyColor2 = [UIColor colorWithRed:0.85098039 green:0.56470588 blue:0.35686275 alpha:1];
            break;
        case 3:
            _EnemyColor = [UIColor colorWithRed:0.45882353 green:0.6 blue:0.70196078 alpha:1];
            _EnemyColor2 = [UIColor colorWithRed:0.57254902 green:0.65882353 blue:0.74117647 alpha:1];
            break;
    }

The problem I am having is that the program only fails if _teamColor2 is set using colorWithRed: Green: Blue: Alpha:. The error I am getting is

-[UIDeviceRGBColor set]: message sent to deallocated instance 0x5f4af80 . I can use redColor instead, and it will work. Is there a reason that only one out of four isn't working properly?

Upvotes: 1

Views: 766

Answers (1)

Anomie
Anomie

Reputation: 94794

None of those should work, because none of them are being retained ([UIColor colorWithRed:green:blue:alpha:] returns an autoreleased instance). Try assigning to self.TeamColor, self.TeamColor2, and so on instead.

Upvotes: 5

Related Questions