Title: Core Graphics Throws EXC_BAD_ACCESS Only On Real Device
Tags: iphone;objective-c;ios;uitableview;exc-bad-access
Question: 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,
Comment: I don't know why it doesn't happen on the simulator, but what does `UIGraphicsGetCurrentContext()` return? You aren't checking the return value, so maybe it's returning NULL?

Comment: I already found out the answer, I'm going to post it for anyone else that views this

Here is the accepted answer: 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:
(__bridge id)
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:
NSArray* array = [NSArray arrayWithObjects:
(id)[UIColor whiteColor]CGColor],
(id)[[UIColor colorWithRed:235.0/255.0
green:235.0/255.0 blue:235.0/255.0]CGColor],nil];
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!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:12/31/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<iphone><objective-c><ios><uitableview><exc-bad-access>', 'Title': 'Core Graphics Throws EXC_BAD_ACCESS Only On Real Device', 'ViewCount': 367, 'id': 14096989, 'user_AboutMe': "<p>Student at Cypress Bay High, realizing that my life calling is based upon <code>std::atomic</code>. Feel free to drop me a line, just make sure there's some form of memory fence after whatever you deem important to guarantee I'll see it. I always <code>std::atomic_thread_fence(std::memory_order_acquire)</code> before reading anything. </p>\n\n<p>Make C++ great again!</p>\n\n<p><code>std::atomic_thread_fence(std::memory_order_release);</code></p>\n", 'user_AccountId': 1330133.0, 'user_DisplayName': 'Alejandro', 'user_Reputation': 3020.0, 'user_Views': 534.0, 'user_id': 1274223.0}}