On iOS, is doing CGContextRelease required?

1.6k views Asked by At

Because if Instruments is run and Activities Monitor is chosen, the app running on iPhone 4S is using 4.88MB if the contexts are released, and is also 4.88MB if the contexts are not released. So does that mean releasing context is optional? (I thought it is required actually). The contexts were referenced by CGContextRef variables. (ARC is being used).

The contexts were CGBitmapContext, created for the Retina display, so at about 640 x 640, and there are 4 such contexts, which are all created in viewDidAppear, and I thought if 1 pixel is 4 bytes, then each context will be 1.6MB already. Could it be that after the viewDidAppear is done, the contexts were automatically released? Basically, I generated CGImage objects from those bitmap contexts and set the CGImage objects to be pointed to by the CALayer objects (using layer.contents = (__bridge id) cgImage;), so the bitmap contexts were no longer required. It is compiled using Xcode 4.3, with ARC, and targeted towards iOS 4.3. (but I thought CGContextRef is not part of ARC).

update: correction: it should be "generate CGImage objects from those CGBitmapContext, and set those CGImage objects to CALayer" (the original question is edited to reflect that).

1

There are 1 answers

10
wjl On BEST ANSWER

Releasing a CGContextRef is not optional, but you should be aware of whether or not you need to release it. It follows the standard manual memory management rules. If you take ownership (alloc, create, retain, and a few others) you must release ownership (release). If you release when you don't have ownership, it's an overrelease.

What you're probably seeing is that even after you've released the object, someone else is retaining it. It's probably something in your view hierarchy. An object not releasing after you release your ownership is typically not a problem.