Good evening,
I’m just about finished with this new app that I made and I was doing some final tests on it before I submitted it to the app store, but something came up that really stumped me. For one of my view controllers I’m using a UITableView and so I implemented the
-(UIView*) tableView:(UITableView*) tableView viewForHeaderInSection:(NSInteger) section
method of the UITableViewDelegate protocol so that I could provide my own custom view for the header. (Yes, I did also conformed to the UITableViewDataSource protocol and provided all necessary methods for it)
So I wrote my own UIView class and implemented the drawRect: method to draw my own custom view. When I run this in both the iPad 6.0 and iPhone 6.0 simulators, it runs perfectly fine.
However, when I plug up my own iOS device ( running iOS 6) it crashes and throws EXC_BAD_ACCESS.
I did some breakpoints and found out that when running the app on my real device, the code only gets executed up to here:
// This code is the beginning of my drawRect method for my custom view
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef lightBlue = [UIColor colorWithRed:72.0/255.0
green:121.0/255.0
blue:201.0/255.0 alpha:1].CGColor;
CGColorRef cream = [UIColor colorWithRed:235.0/255.0
green:235.0/255.0
blue:235.0/255.0 alpha:1].CGColor;
CGContextSetFillColorWithColor(context,cream);
CGContextFillRect(context, _paperBox);
// the _paperBox variable was defined earlier as CGRect _paperBox
// the _paperBox variable was given a value in the -(void)layoutSubviews method
CGColorRef shadow = [UIColor colorWithWhite:.5 alpha:.5].CGColor;
CGContextSetShadowWithColor(context, CGSizeMake(0,3), 2, shadow);
CGContextSetFillColorWithColor(context, lightBlue);
// and later on I setup some code to draw a linear gradient:
NSArray colors = [NSArray arrayWithObjects:(__bridge id) lightBlue,
(__bridge id) cream,
nil];
The EXC_BAD_ACCESS happens right at that last line. Why does this only happen when running on a real iOS device and not on the simulators?
Thanks,
I found out the answer to my question, hopefully anyone else reading this that was having the same troubles will find this response helpful. Due to the release of ARC and the new __bridge modifiers, my old casts to:
on each CGColorRef weren’t technically “correct” with the new ARC terms, and I’m not too familiar with the new __bridge concepts so what I did that fixed it was not to make a CGColorRef for each UIColor, but instead cast the CGColor property of each UIColor to id in the array like so:
This seemed to do the trick for me. However, I do recommend for everyone who isn’t already familiar with the new __bridge modifiers to learn them when using ARC ( including myself).
Thanks everyone!