如果我在Swift中获得UnsafeMutablePointer,我应该免费拨打电话吗?

问题描述 投票:1回答:1

我在一个带有hold和UnsafeMutablePointer<objc_property_t>的函数内部使用变量。我应该打电话给free吗?

func logProps() {
    var count: UInt32 = 0
    let _propArr = class_copyPropertyList(cls, &count)
    // ...
    // free(propArr)  // ?
}

另一方面,free与使用deallocateSwift UnsafeMutablePointer: Must I call deinitialize before deallocate?)相同吗?

swift memory-management
1个回答
3
投票

是的,因为该方法的名称。 class_copyPropertyList包含“复制”一词,表示缓冲区属于您。请注意,documentation也表明:

您必须使用free()释放数组。

所以你应该使用free()来破坏这个缓冲区。这与调用.deallocate()but that is not promised相同,因此请按照给出的说明进行操作。

(感谢MartinR解决了free vs deallocate的问题。)

© www.soinside.com 2019 - 2024. All rights reserved.