18

I've created a context using CGBitmapContextCreate. Do I need to release it using CGContextRelease? I know the answer is yes in Objective-C, but how about in Swift?

Thanks!

user2732722
  • 615
  • 1
  • 10
  • 22
  • Yes, you need to follow the same rules in Swift as in Objective C – sapi Jan 10 '15 at 04:25
  • Thanks! So does it mean that I should also release the CGImageRef? I have the following two lines of code: var imageRef = CGBitmapContextCreateImage(context); var image = UIImage(CGImage: imageRef) Should I also release imageRef? – user2732722 Jan 10 '15 at 04:31
  • @sapi Are you sure they follow the same rules? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html – rakeshbs Jan 10 '15 at 04:42
  • I've just written an online Swift tutorial that covers this: http://www.apeth.com/swiftBook/apa.html#_cftyperefs – matt Jan 10 '15 at 04:44
  • Basically everything is memory-managed for you unless it arrives as an Unmanaged generic - and in that case, you will know because you won't be able to proceed until you add memory management. – matt Jan 10 '15 at 04:46
  • Yes, you don't need to release it in Swift. I just added the release to my program and got a nice compiler error: 'CGContextRelease' is unavailable: Core Foundation objects are automatically memory managed – user2732722 Jan 10 '15 at 04:48
  • 1
    @rakeshbs My mistake; things seem to have changed since I last played with this (at which point very few API calls were annotated) – sapi Jan 10 '15 at 05:37

2 Answers2

28

CFTypes are automatically managed unless explicitly specified as Unmanaged.
According to the documentation. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

Core Foundation objects returned from annotated APIs are automatically memory managed in Swift—you do not need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself. If you return Core Foundation objects from your own C functions and Objective-C methods, annotate them with either CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED. The compiler automatically inserts memory management calls when it compiles Swift code that invokes these APIs. If you use only annotated APIs that do not indirectly return Core Foundation objects, you can skip the rest of this section. Otherwise, continue on to learn about working with unmanaged Core Foundation objects.

When Swift imports APIs that have not been annotated, the compiler cannot automatically memory manage the returned Core Foundation objects. Swift wraps these returned Core Foundation objects in an Unmanaged structure.

Unmanaged types will have the type signature

func StringByAddingTwoStrings(CFString!, CFString!) -> Unmanaged<CFString>!

CGBitmapContextCreate has the type signature

func CGBitmapContextCreate(...) -> CGContext!

Hence its managed automatically by swift.

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
12

No, you don't need to call CGContextRelease. In fact, trying to gives you this error:

'CGContextRelease' is unavailable: Core Foundation objects are automatically memory managed

CGContext instances are automatically memory managed in Swift. You can tell from the function signature:

func CGBitmapContextCreate(/* several parameters */) -> CGContext!

A return value you would need to release yourself would look like:

func CGBitmapContextCreate(/* several parameters */) -> Unmanaged<CGContext>!
Nate Cook
  • 92,417
  • 32
  • 217
  • 178