Swift 4.1 deinitialize and deallocate(capacity:) deprecated
Asked Answered
M

1

5

I've been saying this to form a C array of CGPoint:

let arr = UnsafeMutablePointer<CGPoint>.allocate(capacity:4)
defer {
    arr.deinitialize()
    arr.deallocate(capacity:4)
}
arr[0] = CGPoint(x:0,y:0)
arr[1] = CGPoint(x:50,y:50)
arr[2] = CGPoint(x:50,y:50)
arr[3] = CGPoint(x:0,y:100)

Now (Swift 4.1 in the Xcode 9.3 beta) both deinitialize and deallocate(capacity:) are deprecated. It looks like what I'm supposed to say now might be:

defer {
    arr.deinitialize(count:4)
    arr.deallocate()
}

Is that right?

Michaeline answered 1/2, 2018 at 19:22 Comment(7)
Note you don't have to call .deinitialize as CGPoint is a trivial type (if it wasn't a trivial type, i.e contained references, then you'd have to initialise the allocated memory before performing assignment, and then deinitialise afterwards).Responsibility
@Responsibility Yes, I'm always confused about this so I always say deinitialize just in case.Michaeline
@Responsibility see for example #46814957 - this is what I was "taught" too, just deinitialize because why notMichaeline
Fair enough; though to be 100% foolproof, you would've wanted to have initialised the memory first by using one of the .initialize methods (before assignment through subscript, that is). You could also use a buffer pointer (gist.github.com/hamishknight/57320e2dbfbd0ad3ef68c69a97830eac).Responsibility
@Responsibility See, this is what I find so confusing. There you are doing the deinitialize on the baseAddress. But my code doesn't do that. So is my code wrong? I'm not getting any closer to clarity or certainty.Michaeline
No your code isn't wrong. In the gist code I'm using a buffer pointer, which is just a pointer + count pair (the pointer component is called baseAddress). The deinitialisation of the baseAddress in my code is equivalent to the arr.deinitialize(count:4) that you do in your code. Buffer pointers don't currently expose a deinitialize() method, so that's why we have to extract the pointer and call it ourselves.Responsibility
It seems (from the defer statement) that the array is used only in some local scope. Is there a special reason not to use Swift array let arr = [CGPoint(x:0,y:0), ...] directly? You still can pass that to a C function, if necessary.Humorous
H
7

Yes, that is part of SE-0184 Unsafe[Mutable][Raw][Buffer]Pointer: add missing methods, adjust existing labels for clarity, and remove deallocation size, which has been implemented in Swift 4.1.

In particular:

Removing capacity from deallocate(capacity:) will end the confusion over what deallocate() does, making it obvious that deallocate() will free the entire memory block at self, just as if free() were called on it.

The old deallocate(capacity:) method should be marked as deprecated and eventually removed since it currently encourages dangerously incorrect code.

Humorous answered 1/2, 2018 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.