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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:12:55+00:00 2026-05-27T19:12:55+00:00

I am getting the error EXC_BAD_ACCESS on the line CGContextClearRect(c, self.view.bounds) below. I can’t

  • 0

I am getting the error EXC_BAD_ACCESS on the line CGContextClearRect(c, self.view.bounds) below. I can’t seem to figure out why. This is in a UIViewController class. Here is the function I am in during the crash.

- (void)level0Func {
printf("level0Func\n");
frameStart = [NSDate date];

UIImage *img;
CGContextRef c = startContext(self.view.bounds.size);
printf("address of context: %x\n", c);
/* drawing/updating code */ {
    CGContextClearRect(c, self.view.bounds); // crash occurs here
    CGContextSetFillColorWithColor(c, [UIColor greenColor].CGColor);
    CGContextFillRect(c, self.view.bounds);

    CGImageRef cgImg = CGBitmapContextCreateImage(c);
    img = [UIImage imageWithCGImage:cgImg]; // this sets the image to be passed to the view for drawing
    // CGImageRelease(cgImg);
}
endContext(c);


}

Here are my startContext() and endContext():

CGContextRef createContext(int width, int height) {
CGContextRef r = NULL;
CGColorSpaceRef colorSpace;
void *bitmapData;
int byteCount;
int bytesPerRow;

bytesPerRow = width * 4;
byteCount = width * height;

colorSpace = CGColorSpaceCreateDeviceRGB();
printf("allocating %i bytes for bitmap data\n", byteCount);
bitmapData = malloc(byteCount);

if (bitmapData == NULL) {
    fprintf(stderr, "could not allocate memory when creating context");
    //free(bitmapData);
    CGColorSpaceRelease(colorSpace);
    return NULL;
}

r = CGBitmapContextCreate(bitmapData, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);

return r;
}

CGContextRef startContext(CGSize size) {
    CGContextRef r = createContext(size.width, size.height);
    // UIGraphicsPushContext(r); wait a second, we dont need to push anything b/c we can draw to an offscreen context
    return r;
}

void endContext(CGContextRef c) {
    free(CGBitmapContextGetData(c));
    CGContextRelease(c);
}

What I am basically trying to do is draw to a context that I am not pushing onto the stack so I can create a UIImage out of it. Here is my output:

wait_fences: failed to receive reply: 10004003
level0Func
allocating 153600 bytes for bitmap data
address of context: 68a7ce0

Any help would be appreciated. I am stumped.

  • 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-27T19:12:56+00:00Added an answer on May 27, 2026 at 7:12 pm

    You’re not allocating enough memory. Here are the relevant lines from your code:

    bytesPerRow = width * 4;
    byteCount = width * height;
    bitmapData = malloc(byteCount);
    

    When you compute bytesPerRow, you (correctly) multiply the width by 4 because each pixel requires 4 bytes. But when you compute byteCount, you do not multiply by 4, so you act as though each pixel only requires 1 byte.

    Change it to this:

    bytesPerRow = width * 4;
    byteCount = bytesPerRow * height;
    bitmapData = malloc(byteCount);
    

    OR, don’t allocate any memory and Quartz will allocate the correct amount for you, and free it for you. Just pass NULL as the first argument of CGBitmapContextCreate:

    r = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im getting a EXC_BAD_ACCESS error on this line. [(CCMenuItemSprite*)[profileSelectionMenu getChildByTag:333] setNormalImage:normalSprite3]; Basically im just
I am getting crazy over this error. Compiler is saying out of scope for
Im getting this error, what could this be??? Program received signal EXC_BAD_ACCESS, Could not
I'm getting a EXC_BAD_ACCESS error when this code gets called NSLog(@Count: %@, [fetchedObjects count]);
I don't know why I'm getting this error. I'm also getting an EXC_BAD_ACCESS issue
My iPhone app is crashing and getting EXC_BAD_ACCESS error when using this code: NSError
hi im new to c# and was trying to code but getting error can
hi i hav a code like this in the end i m getting error
I'm having a bear of a time trying to figure out why I'm getting
I'm getting the following error Program received signal: EXC_BAD_ACCESS. warning: check_safe_call: could not restore

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.