且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

您需要在Swift中释放CGContextRef吗?

更新时间:2023-09-22 09:45:16

除非明确指定为Unmanaged,否则将自动管理CFType.
根据文档. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

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

从注释的API返回的

Core Foundation对象是自动的 在Swift中管理的内存-您无需调用CFRetain, CFRelease或CFAutorelease自己起作用.如果您退还Core 来自您自己的C函数和Objective-C方法的基础对象, 用CF_RETURNS_RETAINED或注释它们 CF_RETURNS_NOT_RETAINED.编译器自动插入内存 在编译调用这些API的Swift代码时,管理调用. 如果仅使用不间接返回Core的带注释的API 基础对象,您可以跳过本节的其余部分.否则, 继续学习有关与非托管Core Foundation一起工作的信息 对象.

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.

当Swift导入未注释的API时,编译器 无法自动内存管理返回的Core Foundation 对象. Swift将这些返回的Core Foundation对象包装在一个 非托管结构.

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具有类型签名

func CGBitmapContextCreate(...) -> CGContext!

因此可以迅速对其进行自动管理.

Hence its managed automatically by swift.

推荐文章