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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:47:34+00:00 2026-06-13T01:47:34+00:00

I am using objective C in a c++ application to load images on OSX.

  • 0

I am using objective C in a c++ application to load images on OSX. It is an OpenGL application. This is the code I use to load an image into opengl:

bool OSXSpecifics::loadPNGOSX(const char *fileName, int &outWidth, int &outHeight, GLuint *outTexture) {
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[NSString stringWithUTF8String: fileName]];
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];

NSArray* imageReps = [image representations];

bool hasAlpha;

for (NSImageRep *imageRep in imageReps) {
    if ([imageRep pixelsHigh] > outHeight) {
        outHeight = [imageRep pixelsHigh];
    }
    if ([imageRep pixelsWide] > outWidth) {
        outWidth = [imageRep pixelsWide];
    }
}
if ([bitmap hasAlpha] == YES) {
    hasAlpha = true;
} else {
    hasAlpha = false;
}


glGenTextures(1, outTexture);
glBindTexture(GL_TEXTURE_2D, *outTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, hasAlpha ? 4 : 3, outWidth, outHeight, 0,
             hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, [bitmap bitmapData]);
[bitmap release];
[image release];
return true;
}

Thanks to Instruments I have detected a memory leak every time this function is called. Instruments says [NSImage TIFFRepresentation] and [NSBitmapImageRep bitmapData] are using up a huge amount of memory.

It does not climb out of control but everytime this function is called the memory usage climbs a few hundred kilobytes.

My experience with objective-c is limited and so I do not know how to fix this as I thought release would work fine.

Just so you know the OpenGL texture is freed at a later point and I have confirmed it is not the source of the memory leak.

EDIT: After some further testing I have discovered this is not theoretically a memory leak as Instruments is not reporting a “memory leak” as such. But whenever this function is called the memory usage of the application goes up and never comes down. This function is called when scenes in the game are loaded, and so whenever a scene is loaded memory usage goes up several megabytes. How could this possibly be happening. I am sure it is this function that is using up all the memory.

  • 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-13T01:47:36+00:00Added an answer on June 13, 2026 at 1:47 am

    First off: The code doesn’t look like it is leaking. My guess is that NSImage is doing some harmless caching internally that ensures it can re-use the same NSImage if you ever load this image file a second time, and that’s maybe what you’re seeing. Or maybe it’s the file system cache. If memory gets tight, these caches will probably get flushed and the freed memory becomes available to your application.

    That said, your code seems overly complicated. You create an NSImage (which consists of NSImageReps, one of which is very likely an NSBitmapImageRep) then get its TIFFRepresentation (i.e. re-encode all that data to TIFF, possibly compressing it again), then create an NSBitmapImageRep from that (decompressing and decoding the TIFF again), and then getting its bitmapData.

    At the least, you should probably create an NSData from the file and create your NSBitmapImageRep from that. That will skip the entire NSImage creation and TIFF steps.

    Even better would probably be if you just went and used the lower-level ImageIO library. Create a CGImageRef from your file, then hand the image’s raw, decompressed data off to OpenGL. I vaguely remember there might even be a better way to create an OpenGL texture if you go through CIImage or CoreVideo, but I may be mixing that up with the reverse (texture to CIImage).

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

Sidebar

Related Questions

I am working on an OSX application using Objective-C, and one of the things
I am writing an application using Objective-C for iPhone. In this application, the user,
I have a compiled external library that I'm using in my (Objective-C++) code. This
I'm trying to load a CSV into a DataTable using this: class CSVReader {
I am starting to learn how to code a mac application using Objective C.
Anyone knows how to convert .jpg image to .bmp format in iphone using objective-C?
I am trying to develop an application in xcode using objective c.When i build
I want to create native Mac OS X application using Cocoa + Objective C
I have the following which opens TextEdit using a Cocoa objective-c application: [[NSWorkspace sharedWorkspace]
I'm writing an Objective-C application using ARC and it's definitely a great improvement even

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.