John Doe
John Doe

Reputation: 2501

Should I call free if I am getting an UnsafeMutablePointer in Swift?

I am using a variable inside a function with holds and UnsafeMutablePointer<objc_property_t>. Should I call free on it?

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

On a different note, is free same as using deallocate (Swift UnsafeMutablePointer: Must I call deinitialize before deallocate?) ?

Upvotes: 1

Views: 244

Answers (1)

Rob Napier
Rob Napier

Reputation: 299565

Yes, because of the name of the method. class_copyPropertyList includes the word "copy" which indicates that the buffer belongs to you. Note that the documentation also indicates this:

You must free the array with free().

So you should use free() to destroy this buffer. This is currently identical to calling .deallocate(), but that is not promised, so follow the instructions as given.

(Thanks to MartinR for running down the question of free vs deallocate.)

Upvotes: 3

Related Questions