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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:27:43+00:00 2026-06-13T07:27:43+00:00

What i am doing is to draw a simple rectangle and set color for

  • 0

What i am doing is to draw a simple rectangle and set color for the painted area

   // Just added
    @interface Gradient () {
       CGColorRef  lightBlueColor;
   }
    @implementation Gradient

    -(id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor   =   [UIColor lightGrayColor];
            NSLog(@"frame is %@",NSStringFromCGRect(self.frame));
            NSLog(@"bound is %@",NSStringFromCGRect(self.bounds));
            lightBlueColor         =   [UIColor colorWithRed:105.0f/255.0f green:179.0f/255.0f blue:216.0f/255.0f alpha:1.0].CGColor;

        }
        return self;
    }

    -(void) layoutSubviews {
        paperRect =   CGRectMake(10, 10, self.bounds.size.width/2, self.bounds.size.height/2);
    }

    -(void)drawRect:(CGRect)rect {

        //Draw a retangle
        CGContextRef        context     =   UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, lightBlueColor);
        CGContextFillRect(context, paperRect);  


    }

Below is what is displayed on the simulator

enter image description here

However, when I am trying to install on the device, I am getting EXC_BAD_ACCESS
enter image description here

Question :

why it does not work on the device. Did I make a mistake somewhere

Edit : I have just tried to modified lightBlueColor

lightBlueColor = [UIColor blueColor].CGColor;

Then I can run the app on the device. I dont get it at all

  • 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-13T07:27:45+00:00Added an answer on June 13, 2026 at 7:27 am

    Edit: so you just ran into a memory management issue. In theory, in both of the cases, the program could have crashed. Why? Because as per the documentation, -[UIColor lightBlueColor] and its accompaining class methods return an autoreleased object. In practice, for optimization reasons, it’s not true: in fact, they return the same alloc-initied singleton in order to save memory. This optimization is possible because they always return the same color. As the shared instance (singleton color) has been created without an autorelease, it doesn’t suddenly get released when you wouldn’t expect that, that’s why the program worked. If Apple’s programmers weren’t smart enough to make this optimization, both programs would have crashed.

    But in the second case, the colorWithRed:green:blue:alpha: method cannot possibly have this optimization, since it’s not guaranteed that it always returns the same color. (Imagine what would happen if you called it to obtain a red color first, then it cached it, then you wanted to obtain a blue color, but it would have returned the cached red one.) So it actually creates a new instance of a color and autoreleases it. But since you don’t retain it, soon it gets deallocated because of the autorelease and thus its CGColor property is invalidated as well. So, there are three possible solutions:

    One. I’d prefer this. Make the lightBlueColor instance variable an UIColor object and create it using

    lightBlueColor = [[UIColor alloc] initWithRed:r green:g blue:b alpha:a];
    

    then just use its CGColor property for drawing.

    Two. Similar to the first one, but you can create the color object as

    lightBlueColor = [[UIColor colorWithRed:r green:g blue:b alpha:a] retain];
    

    as well, however I’d say this is a wrong concept.

    Three: you can make the UIColor object vanish in the deep well of autorelease pools, but keep the CGColor safe:

    lightBlueColor = CGColorRetain([UIColor colorWithRed:r green:g blue:b alpha:a].CGColor);
    

    In each case, you should pay attention to memory management in the dealloc method.

    Lesson learned: instance variables aren’t good for storing autoreleased objects. You want to alloc-init and release when appropriate.

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

Sidebar

Related Questions

I'm trying to create a simple window in Win32 and draw a rectangle in
I am doing simple (for now) application to draw on canvas. I can draw
I just want to draw simple 2D objects like circle, line, square etc in
I'm am doing a simple coding attempt trying to draw on a SurfaceView created
I don't understand what I'm doing wrong here: public void Draw(GameTime gameTime) // in
I am doing an Eclipse plugin using gef. Does anybody knows how to draw
Doing some jquery animation. I have certain divs set up with an attribute of
I have an OpenGL-related issue. Whenever I attempt to draw a simple polygon using
I'm doing this paint application. It's kind of simple. It consist of a panel
I have a small application that uses a DrawingArea to draw a simple map

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.