What i am doing is to draw a simple rectangle and set color for the painted area
// Just added
@interface Gradient () {
CGColorRef lightBlueColor;
}
@implementation Gradient
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor lightGrayColor];
NSLog(@"frame is %@",NSStringFromCGRect(self.frame));
NSLog(@"bound is %@",NSStringFromCGRect(self.bounds));
lightBlueColor = [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:1.0].CGColor;
}
return self;
}
-(void) layoutSubviews {
paperRect = CGRectMake(10, 10, self.bounds.size.width/2, self.bounds.size.height/2);
}
-(void)drawRect:(CGRect)rect {
//Draw a retangle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, lightBlueColor);
CGContextFillRect(context, paperRect);
}
Below is what is displayed on the simulator

However, when I am trying to install on the device, I am getting EXC_BAD_ACCESS

Question :
why it does not work on the device. Did I make a mistake somewhere
Edit : I have just tried to modified lightBlueColor
lightBlueColor = [UIColor blueColor].CGColor;
Then I can run the app on the device. I dont get it at all
Edit: so you just ran into a memory management issue. In theory, in both of the cases, the program could have crashed. Why? Because as per the documentation,
-[UIColor lightBlueColor]and its accompaining class methods return an autoreleased object. In practice, for optimization reasons, it’s not true: in fact, they return the same alloc-initied singleton in order to save memory. This optimization is possible because they always return the same color. As the shared instance (singleton color) has been created without an autorelease, it doesn’t suddenly get released when you wouldn’t expect that, that’s why the program worked. If Apple’s programmers weren’t smart enough to make this optimization, both programs would have crashed.But in the second case, the
colorWithRed:green:blue:alpha:method cannot possibly have this optimization, since it’s not guaranteed that it always returns the same color. (Imagine what would happen if you called it to obtain a red color first, then it cached it, then you wanted to obtain a blue color, but it would have returned the cached red one.) So it actually creates a new instance of a color and autoreleases it. But since you don’t retain it, soon it gets deallocated because of the autorelease and thus itsCGColorproperty is invalidated as well. So, there are three possible solutions:One. I’d prefer this. Make the
lightBlueColorinstance variable anUIColorobject and create it usingthen just use its
CGColorproperty for drawing.Two. Similar to the first one, but you can create the color object as
as well, however I’d say this is a wrong concept.
Three: you can make the UIColor object vanish in the deep well of autorelease pools, but keep the
CGColorsafe:In each case, you should pay attention to memory management in the
deallocmethod.Lesson learned: instance variables aren’t good for storing autoreleased objects. You want to alloc-init and release when appropriate.