Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imageWithCGImage and memory

If I use [UIImage imageWithCGImage:], passing in a CGImageRef, do I then release the CGImageRef or does UIImage take care of this itself when it is deallocated?

The documentation isn't entirely clear. It says "This method does not cache the image object."

Originally I called CGImageRelease on the CGImageRef after passing it to imageWithCGImage:, but that caused a malloc_error_break warning in the Simulator claiming a double-free was occurring.

like image 485
Adam Ernst Avatar asked Sep 09 '09 21:09

Adam Ernst


2 Answers

According to the fundamental rule of Cocoa memory management, an owning object should release the owned object when it no longer needs it. Other objects are responsible for taking and releasing ownership on their own. If a UIImage needs an object to persist but doesn't retain or copy it, it's a bug in UIImage's implementation and should be reported as such.

like image 99
outis Avatar answered Nov 02 '22 03:11

outis


I have the same problem in XCode 3.2.1 on Snow Leopard. I have pretty much the same code as Jason.

I have looked at the iPhone sample code which uses imageWithCGImage and they always release the CGImageRef using CGImageRelease after a call to imageWithCGImage.

So, is this a bug in the simulator? I always get the malloc_error_break warning on the console when I use CGImageRelease.

like image 32
Lextar Avatar answered Nov 02 '22 03:11

Lextar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!