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 7765173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:06:15+00:00 2026-06-01T15:06:15+00:00

I’m creating an application for iOS 5 and I’m drawing some gradients. The following

  • 0

I’m creating an application for iOS 5 and I’m drawing some gradients. The following gradient code I’ve always used before ARC, but now it does not work on my device anymore (however, it works on the simulator) when I use it several times (so I suppose it’s a memory management issue). Anyways, here’s the code:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };

NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];

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);

Originally, there were no __bridge statements, I added them as suggested by Xcode. What exactly is causing the problem?

  • 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-06-01T15:06:16+00:00Added an answer on June 1, 2026 at 3:06 pm

    Your issue might be with the lifetime of the startColor, etc. variables. I’m guessing that these might be CGColorRefs created via UIColor’s -CGColor method at some point above the code you’ve listed.

    As I describe in this answer, unless you explicitly retain those CGColorRefs, they may go away after the UIColor that generated them has been deallocated. Given that you never use the UIColors again after you’ve extracted the CGColorRefs from them, ARC may decide to deallocate these UIColors before you’ve had a chance to use the CGColorRefs. I’ve seen object lifetimes differ between the Simulator and actual devices, so this could explain the crash on one but not the other.

    My solution to this has been to use an immediate cast to id, like in the following:

    NSArray *colors = [NSArray arrayWithObjects:(id)[color1 CGColor],
                                                (id)[color2 CGColor], nil];
    

    where the compiler does the right thing as far as transferring ownership of the CGColorRefs.

    There’s also the possibility that your NSArray is being deallocated early, in which case the following code might make sure it hangs around long enough:

    NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
    
    CFArrayRef colorArray = (__bridge_retained CFArrayRef)colors;
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colorArray, locations);
    CFRelease(colorArray);
    

    This bridges the NSArray across to Core Foundation, leaving behind a CFArrayRef with a retain count of at least 1. You can then use that in your gradient creation, where the gradient will hopefully keep a reference to it, and release it manually when done.

    However, Bringo’s suggestion to work entirely within Core Graphics’ C API for this might be the easiest way to go here. I just thought I’d explain a potential source of your problems, in case you run into something similar in the future.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.