6

I had this:

let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?

Which now I get the warning:

Initialization of 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer') results in a dangling pointer

Detailed warning consists of:

  1. Implicit argument conversion from '[vImagePixelCount]' (aka 'Array') to 'UnsafePointer' (aka 'UnsafePointer') produces a pointer valid only for the duration of the call to 'init(mutating:)'

  2. Use the 'withUnsafeBufferPointer' method on Array in order to explicitly convert argument to buffer pointer valid for a defined scope

Is there a way around this?

2
  • 2
    You just need to follow the instructions shown in the Detailed warning. Commented Mar 26, 2020 at 15:23
  • 3
    This was always wrong, now you’re busted. Be happy! Commented Mar 27, 2020 at 0:34

3 Answers 3

11

Try this

var bytes = [UInt8]()
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: bytes.count)
uint8Pointer.initialize(from: &bytes, count: bytes.count)
Sign up to request clarification or add additional context in comments.

3 Comments

Where is alpha, the variable mentioned in the question's code?
Legit this worked for me! Thank you. btw Scotty, uint8Pointer == alphaPtr.
awesome! I was struggling with crash due to UnsafeMutablePointer ! your answer helped me thank you @rajasekhar-pasupuleti I had an array pixelBuffer - [Float32] so I did this let floatPointer = UnsafeMutablePointer<Float32>.allocate(capacity:pixelBuffer.count) floatPointer.initialize(from: &pixelBuffer, count: pixelBuffer.count)
7

It was never safe to do this, and the compiler now is warning you more aggressively.

let alphaPtr = UnsafeMutablePointer ...

At the end of this line, alphaPtr is already invalid. There is no promise that what it points to is still allocated memory.

Instead, you need to nest whatever usage you need into a withUnsafeMutablePointer() (or withUnsafePointer()) block. If you cannot nest it into a block (for example, if you were storing the pointer or returning it), there is no way to make that correct. You'll have to redesign your data management to not require that.

Comments

2

Do you need use the withUnsafeBufferPointer method from Array as

var alphaPtr: UnsafeBufferPointer = alpha.withUnsafeBufferPointer { $0 }

that's command produce a pointer optional if you need working with a specific type could you you use bindMemory(to:) or other function that match with you requirements.

Sometimes use a &alpha if you need a UnsafeRawPointer as a function parameter.

2 Comments

This is not any more correct than the original code. There is no promise that alphaPtr is valid after the closing brace. If you use alphaPtr in any way it's undefined behavior, which is what the warning is telling you.
that gets me: Cannot convert value of type 'UnsafeBufferPointer<UInt8>' to closure result type 'UnsafePointer<UInt8>?'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.