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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:06:05+00:00 2026-05-17T21:06:05+00:00

Trying to find the leak in my code I removed all the thing that

  • 0

Trying to find the leak in my code I removed all the thing that did not matter, leaving the following code:

static int last_memory = 0;

void report_memory(NSString *name) {
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                                   TASK_BASIC_INFO,
                                   (task_info_t)&info,
                                   &size);
    if( kerr == KERN_SUCCESS ) {
        int change = (int)info.resident_size - last_memory;
        NSLog(@"%@ >>> Memory in use: %u (change: %d Kb)", name, info.resident_size, change/1024 );
        last_memory = info.resident_size;
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
}

- (CGContextRef) createBitmapContextOfSize:(CGSize) size {
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        NSLog(@"Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     size.width,
                                     size.height,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    CGContextSetAllowsAntialiasing (context,NO);
    if (context== NULL) {
        free (bitmapData);
        CGColorSpaceRelease( colorSpace );
        NSLog (@"Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );
    return context;
}

- (IBAction) clickButton:(UIButton *)sender {
    report_memory(@"begin");
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];

    report_memory(@"image_created");

    CGContextRef myBitmapContext = [self createBitmapContextOfSize:CGSizeMake(256, 256)];
    if (!myBitmapContext){
        return;
    }
    report_memory(@"bitmap_context_created");
    CGContextDrawImage(myBitmapContext, CGRectMake(0, 0, 256, 256), image.CGImage);
    report_memory(@"draw_image");
    CGImageRef ref = CGBitmapContextCreateImage(myBitmapContext);
    report_memory(@"image_context_created");
    CGContextRelease(myBitmapContext);
    report_memory(@"bitmap_context_released");
    UIImage *result = [UIImage imageWithCGImage:ref];
    report_memory(@"image_created");
    CGImageRelease(ref);
    report_memory(@"image_context_released");   
    imageView.image = result;
    report_memory(@"image_assigned");   
}

(“test” is 256×256 JPEG image).
The console showed the following after a few clicks:

begin >>> Memory in use: 18866176 (change: -12 Kb)
image_created >>> Memory in use: 18870272 (change: 4 Kb)
bitmap_context_created >>> Memory in use: 18870272 (change: 0 Kb)
draw_image >>> Memory in use: 19140608 (change: 264 Kb)
image_context_created >>> Memory in use: 19140608 (change: 0 Kb)
bitmap_context_released >>> Memory in use: 19140608 (change: 0 Kb)
image_created >>> Memory in use: 19140608 (change: 0 Kb)
image_context_released >>> Memory in use: 19140608 (change: 0 Kb)
image_assigned >>> Memory in use: 19140608 (change: 0 Kb)

A net loss of 256Kb per click (equal to 256x256x4). The same trend could be seen in the allocations performance tool. Everything pointing to the CGBitmapContext not being released. I just don’t see what I am doing wrong…

  • 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-17T21:06:06+00:00Added an answer on May 17, 2026 at 9:06 pm

    Let the CGBitmapContextCreate allocate the memory itself by providing NULL as the first argument (bitmapData). That frees you from managing your memory allocation – which you’re not freeing at the moment.

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

Sidebar

Related Questions

I am trying to find a memory leak using ants memory profiler, and I've
I've been spending the last few hours trying to find the memory leak in
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to find an example that has css rollover using sprites & sliding door
Trying to find examples of when decorators might be really beneficial, and when not
Trying to find an efficient way to extract all instances of items in an
I am trying to find memory leak with Visual Leak Detector. It shows me
I have been trying to find memory leak in my application for a week
I am trying find lat & long from user given address, for that i
I'm trying to resolve a memory leak but I can't find any solution. Instruments

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.