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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:39:46+00:00 2026-06-17T19:39:46+00:00

I have the memory leak problem with CGImageRef (I guess, it’s so) in my

  • 0

I have the memory leak problem with CGImageRef (I guess, it’s so) in my Cocoa Desktop Application. I’ve read lots of questions here and anywhere else, read FAQ of Core Graphics memory management on developer.apple.com. Maybe this question is more similar to mine, though solution didn’t help.

My task is to scale area 15*15 from saved CGImage and return NSImage* as a result, it is done on each mouse movement.

-(NSImage*)getScaledAreaInX:(int)x andY:(int)y
{
    // Catching image from the screen
CGImageRef fullscreen = CGImageRetain(_magniHack);
    // Cropping
int screenHeight = CGImageGetHeight(fullscreen);
CGRect fixedRect = CGRectMake(x-7, screenHeight-y-8, 15, 15);
CGImageRef cropped = CGImageCreateWithImageInRect(fullscreen, fixedRect);

    // Scaling
int width = CGImageGetWidth(cropped)*8;    // New width;
int height = CGImageGetHeight(cropped)*8;  // New height;

CGColorSpaceRef colorspace = CGImageGetColorSpace(cropped);
CGContextRef context = CGBitmapContextCreate(NULL, width, height,
                                             CGImageGetBitsPerComponent(cropped),
                                             CGImageGetBytesPerRow(cropped),
                                             colorspace,
                                             CGImageGetAlphaInfo(cropped));
CGContextSetInterpolationQuality(context, kCGInterpolationNone);


CGContextDrawImage(context, CGRectMake(0, 0, width, height), cropped);
CGImageRef scaled = CGBitmapContextCreateImage(context);

    // Casting to NSImage
NSImage *image = [[NSImage alloc] initWithCGImage:scaled size:NSZeroSize];

    // Releasing memory
CGImageRelease(fullscreen);
CGColorSpaceRelease(colorspace);
CGContextRelease(context);
//CGImageRelease(cropped); // Can't do: will crash; In what situations can free?
cropped = NULL;
CGImageRelease(scaled);
scaled = NULL;

return image;
}

If I uncomment CGImageRelease line, app will crash on 6th movement of cursor, on retaining _magniHack or cropping the image (it differs every time), the message is “EXC_BAD_ACCESS”. If I don’t, there will be memory leak every time (during frequent movements the leak is dozens of MB). The same result I get if I release cropped, but do not release scaled image (though leak will be much more).

_magniHack – CGImageRef, it is private instance variable, it is set only once in that code:

 -(void)storeFullScreen
 {
     if (_magniHack) {
         CGImageRelease(_magniHack);
     }
     _magniHack = CGDisplayCreateImage(displays[0]);
 }

I use ARC in project if it helps. Though this thing still can’t help get rid of leaks.
I guess that _magniHack is released somewhere, but I can’t find, where, cause I always implement retain on start and release in the end.

  • 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-17T19:39:47+00:00Added an answer on June 17, 2026 at 7:39 pm

    Actually, I got rid of this leak by using Core Graphics only if necessary, i.e. after grabbing the screen I wrap it into NSImage*, and atter I work only with it. But it is still interesting, what was wrong with the code above.

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

Sidebar

Related Questions

I have a problem with a memory leak in a very large ASP.NET application.
I have a problem with a memory leak in a .NET CF application. Using
I have a memory leak from 3 UIImages the problem is I can't just
I have a problem with a memory leak. I have this code in a
I have a memory leak on one of the part of my node.js application.
I have memory leak in the web application (servlet) that I am working on.
I have a memory leak problem that just can not understand! Watch this initialization
I am running cherrypy on webfactional. I have a serious memory leak problem, and
I have a very strange memory leak problem, it seems that sqlite3_step is doing
Consider an ASP.NET application with has a connection pool memory leak problem (where connections

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.