我正在函数中使用具有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?)相同吗?
答案 0 :(得分:3)
是的,因为该方法的名称。 class_copyPropertyList
包含单词“ copy”,表示缓冲区属于您。请注意,https://www.gravatar.com/avatar/d74600e380dbf727f67113fd71669d88?s=400&d=identicon也表明了这一点:
您必须使用free()释放数组。
因此,您应该使用free()
破坏此缓冲区。当前,这与调用.deallocate()
,documentation相同,因此请按照给出的说明进行操作。
(感谢MartinR解决了free与deallocate的问题。)