Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:22:53+00:00 2026-05-26T23:22:53+00:00

I have a code that compile without problems. It runs well on the iPhone

  • 0

I have a code that compile without problems. It runs well on the iPhone simulator, but on my device, I get an EXC_BAD_ACCESS.

This happens in a helper function to draw gradient. I followed this tutorial to do it. The code I have is as follows:

- (void) drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorRef whiteColor = [UIColor whiteColor].CGColor;
    CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0
                                                green:230.0/255.0 
                                                 blue:230.0/255.0 
                                                alpha:1.0].CGColor;
    CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0
                                                green:208.0/255.0 
                                                 blue:208.0/255.0
                                                alpha:1.0].CGColor;
    CGRect paperRect = self.bounds;
    CGRect nameRect = self.nameLabel.frame;
    CGPoint sepStartPoint = CGPointMake(nameRect.origin.x, 
                                        nameRect.origin.x + nameRect.size.height + 2);
    CGPoint sepEndPoint = CGPointMake(nameRect.origin.x + nameRect.size.width, 
                                      nameRect.origin.x + nameRect.size.height + 2);

    drawLinearGradient(context, paperRect, lightGrayColor, whiteColor);
    draw1PxStroke(context, sepStartPoint, sepEndPoint, separatorColor);

}


// Callee, where the problem is
void drawLinearGradient(CGContextRef context,
                        CGRect rect,
                        CGColorRef startColor, 
                        CGColorRef endColor)
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };

    NSArray *colors = [NSArray arrayWithObjects:
                       (__bridge id)startColor,
                       (__bridge id)endColor,
                       nil]; // Here is the line

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, 
                                                        (__bridge CFArrayRef) colors, locations);

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

    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

Xcode highlights line 12 (the one with nil]; as the error line.

For Peter Hosey, here’s the debugger output:

(gdb) po startColor
<CGColor 0x1deca0> [<CGColorSpace 0x1d3280> (kCGColorSpaceDeviceGray)] ( 1 1 )
Current language:  auto; currently objective-c
(gdb) po endColor
<CGColorSpace 0x1bf120> (kCGColorSpaceDeviceRGB)
(gbd)

My simulator (and iPhone) runs on iOS 5.

What could be causing this crash?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:22:53+00:00Added an answer on May 26, 2026 at 11:22 pm

    One way to work around this would be to pass UIColors into your function, instead of CGColorRefs, and use an (id)[color1 CGColor] cast for each element of your colors array. This appears to be the most popular way that people are addressing this problem right now.

    I point out one use of this in this answer, and there’s extended discussion about this in this Apple developer forum thread. If you use a UIColor’s -CGColor method at the point of declaring your NSArray, and cast to id, everything’s bridged automatically for you. As gparker states in the above-linked forum thread:

    The automatic case described by the documentation applies only to
    calling an Objective-C method that returns a CF type and then
    immediately casting the result to an Objective-C object type. If you
    do anything else with the method result, such as assign it to a
    variable of a CF type, then it is no longer automatic.

    As hatfinch points out, this could mean that your CGColorRefs placed in temporary variables won’t hang around after the last time you reference your UIColors, unless you explicitly retain them. Like the others in that forum thread, I mistakenly thought this was a bug in the bridging implementation, but I can see that I was reading this wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that compiles and runs on MSVC++ but will not compile
I have code that I want to compile on all unix systems, but if
Challenge: I have this code that fails to compile. Can you figure out what's
I have this source code from 2001 that I would like to compile. It
I have the following code that won't compile and although there is a way
Suppose I have some code that would, in theory, compile against any version of
I have following code snippet that i use to compile class at the run
I have the following bit of legacy C++ code that does not compile: #include
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I have a piece of code which compiles without problems with x86 gcc 4.4.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.