Deadfrog
Deadfrog

Reputation: 23

CG Gradient Crash

wondering if someone could help me, but at the same time hopefully I can help someone else too...

I have this problem with a CG Gradient that runs in the simulator but crashes on a device. I have searched and read the "CG Gradient runs on simulator, but not on iPhone" thread and I think its a similar problem, i.e due to me using ARC and it releasing the CGFloat too early, but I can't quite get my head around how to implement the suggested fixes in relation to my code. (Please see the code at the bottom)

So I would be grateful if someone could help me with that part??

Anyway, the crash is so bad that it seems to continue to run the app on device in a really odd state, I can delete the app on the device and try to run it again with the offending code commented out, but it still causes a crash. No matter what I did it would not allow the app to run on the device again. Turns out that after some hours of scratching my head, that I needed to reset the iPad device I was attempting to deploy to as it was still holding onto the provisioning profile despite being closed / deleted from the device.

Hopefully, that makes some sense and can help someone too.

Thanks

CGFloat colors [] = { 
    0, 0, 0, 0, 
    0, 0, 0, 0
};    

CGFloat colors2 [] = { 
    190.00/255.00, 211.00/255.00, 60.00/255.00, 1.0, 
    138.00/255.00, 153.00/255.00, 43.00/255.00, 1.0
};

CGFloat colors3 [] = { 
    159.00/255.00, 164.00/255.00, 39.00/255.00, 1.0,
    110.00/255.00, 120.00/255.00, 27.00/255.00, 1.0 

};


CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient;
if (which_state==@"1") {
    gradient = CGGradientCreateWithColorComponents(baseSpace, colors2, NULL, 2);
    NSLog(@"which state has changed");
} else if (which_state==@"2"){
    gradient = CGGradientCreateWithColorComponents(baseSpace, colors3, NULL, 2);
} else if (which_state==@"3"){
    gradient = CGGradientCreateWithColorComponents(baseSpace, colors3, NULL, 2);
} else if (which_state==@"0"){
    gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
}


CGColorSpaceRelease(baseSpace), baseSpace = NULL;


CGContextSaveGState(context);
CGContextMoveToPoint(context, (width/4*0)+5, 0);
CGContextAddLineToPoint(context, (width/4*0)+30, self.frame.size.height-3);
CGContextAddLineToPoint(context, (width/4*1)+30, self.frame.size.height-3);
CGContextAddLineToPoint(context, (width/4*1)+5, 0);
CGContextAddLineToPoint(context, (width/4*0)+5, 0);
CGContextClip(context);

CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient), gradient = NULL;

CGContextRestoreGState(context);
CGContextFillPath(context);

Upvotes: 2

Views: 376

Answers (1)

jrturton
jrturton

Reputation: 119242

You haven't said how the app is crashing, but this looks wrong:

CGGradientRef gradient; 
if (which_state==@"1")
... 

You are comparing strings using ==, and you should be using if ([which_state isEqualToString:@"1"]). == Checks for pointer equality, not string equality. From the look of your code it might be better to use an enum, but that's a side issue.

If your code goes through that set of if statements without hitting anything (since == may not give you a match) then you have an uninitialized CGGradientRef, which you later on attempt to draw and release. This would cause a crash.

Upvotes: 2

Related Questions