سؤال

When you create a CGLayer like so, and then get the context...it appears to be impossible to release the CGContextRef?

Releasing the CGLayerRef itself (apparently) works fine.

You'd think you could release the CGContextRef just before releasing the CGLayer - but no? Nor can you release the CGContextRef just after releasing the CGLayer.

If you release the CGContextRef, the app crashes.

CGLayerRef aether = CGLayerCreateWithContext(
    UIGraphicsGetCurrentContext(), CGSizeMake(1024,768), NULL);
CGContextRef weird = CGLayerGetContext(aether);

// paths, strokes, filling etc
// paths, strokes, filling etc

// try releasing weird here
CGLayerRelease(aether);
// or, try releasing weird here

Does anyone know what is going on here? (Note further that CGContextRelease is indeed just the same as CFRelease, with some nil checking.)

In fact should you never manually release CGContextRef? Does anyone know? Cheers.

CGContextRelease(weird); // impossible, not necessary, doesn't work???

Regarding Joel's spectacular answer below:

Is releasing the CGLayerRef correct and proper? Joel has pointed out:
"Yes, since the function you're obtaining it from has 'Create' in its signature. See: documentation/CoreFoundation/"

هل كانت مفيدة؟

المحلول

You do not own the context returned from CGLayerGetContext, and so should not release it*. In particular, see http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/writerid/cfGetRule for information regarding 'Get' functions in Core Foundation.

*: at least, you shouldn't release it given your example code. If you retained it first (CGContextRetain(weird)), then you should have a CGContextRelease to balance it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top