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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    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). – Hamish Feb 01 '18 at 19:37
  • @Hamish Yes, I'm always confused about this so I always say `deinitialize` just in case. – matt Feb 01 '18 at 19:45
  • @Hamish see for example https://stackoverflow.com/questions/46814457/swift-unsafemutablepointer-must-i-call-deinitialize-before-deallocate - this is what I was "taught" too, just `deinitialize` because why not – matt Feb 01 '18 at 19:47
  • 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 (https://gist.github.com/hamishknight/57320e2dbfbd0ad3ef68c69a97830eac). – Hamish Feb 01 '18 at 20:00
  • @Hamish 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. – matt Feb 01 '18 at 20:26
  • 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. – Hamish Feb 01 '18 at 20:38
  • 1
    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. – Martin R Feb 01 '18 at 21:08

1 Answers1

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382