我在一个带有hold和UnsafeMutablePointer<objc_property_t>
的函数内部使用变量。我应该打电话给free
吗?
func logProps() {
var count: UInt32 = 0
let _propArr = class_copyPropertyList(cls, &count)
// ...
// free(propArr) // ?
}
另一方面,free
与使用deallocate
(Swift UnsafeMutablePointer: Must I call deinitialize before deallocate?)相同吗?
是的,因为该方法的名称。 class_copyPropertyList
包含“复制”一词,表示缓冲区属于您。请注意,documentation也表明:
您必须使用free()释放数组。
所以你应该使用free()
来破坏这个缓冲区。这与调用.deallocate()
,but that is not promised相同,因此请按照给出的说明进行操作。
(感谢MartinR解决了free vs deallocate的问题。)