Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
In the iPhone book by Erica Sadun there are several places where she uses the function createImage() to crate an image on the fly with some text or graphics.

Those functions usually end like this:

Code:
   CGImageRef myRef = CGBitmapContextCreateImage (context);
   free(CGBitmapContextGetData(context));
   CGContextRelease(context);

   return [UIImage imageWithCGImage:myRef];

And on the web I found several similar functions, this one for example at iphonedevsdk.com!

So, is there a memory leak? Do they need to call CGImageRelease() before returning an UIImage instance?

Code:
   UIImage  uiImage = [UIImage imageWithCGImage:myRef];
   CGImageRelease (myRef);
   return (uiImage);

Apple's documentation confuses everything with some other problem: CGBitmapContextCreateImage
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Yes, the code you show has a memory leak. CGBitmapContextCreateImage follows the Create Rule and the created CGImage must be released.

Code usually looks something like this:

Code:
	CGImageRef	ref = CGBitmapContextCreateImage(bitmap);
	UIImage*	result = [UIImage imageWithCGImage:ref];

	CGContextRelease(bitmap);	// ok if NULL
	CGImageRelease(ref);

	return result;
 

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
Thanks for your reply.

At first I was sure there was a leak, but wherever I looked there was that same (wrong) pattern and then I wasn't so sure any more.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.