Hello,
This code worked before upgrading to Xcode 11.4.1
///Double Complex vector phase radians
static func phase(Real x:[Double], Imag y:[Double])->[Double] {
guard (x.count == y.count) else {
myLog(toFile: false, toCl:false, message: K.BOMB)
return [0]
}
var real:[Double] = x
var imaginary:[Double] = y
var splitComplex = DSPDoubleSplitComplex(realp: &real, imagp: &imaginary)
var results:[Double] = zeroD(npts:x.count)
vDSP_zvphasD(&splitComplex, 1, &results, t, vDSP_Length(x.count))
return results
}Now the warning
Inout expression creates a temporary pointer, but argument 'imagp' should be a pointer that outlives the call to 'init(realp:imagp:)'
Inout expression creates a temporary pointer, but argument 'realp' should be a pointer that outlives the call to 'init(realp:imagp:)'What changed? Is there a quick fix?
Thanks.
Did you end up filing a bug report?
It is not a bug of Swift, but a bug of your code. Just that old Swift could not detect this sort of misusage and (if you think it was working) your code was seemingly working just by luck.
So I don't see what's up with the warning.
When you pass an Array to a pointer argument as an inout parameter, Swift allocates a temporary region for the Array, and passes the address of the temporary region to the function and call it, after finishing it, Swift releases the temporary region.
So, in your code, after finishing this line, `var splitComplex = DSPDoubleSplitComplex(realp: &real, imagp: &imaginary)`, your `splitComplex` may hold two already released memory region in `realp` and `imagp`.
In some conditions, Swift would pass the current address of the content of the Array, but the condition is not documented and may change in the future, also the current address of the content of the Array is not stable enough.
You should better update your code:
static func phase(Real x:[Double], Imag y:[Double])->[Double] {
guard (x.count == y.count) else {
myLog(toFile: false, toCl:false, message: K.BOMB)
return [0]
}
var real:[Double] = x
var imaginary:[Double] = y
var results:[Double] = zeroD(npts: x.count)
real.withUnsafeMutableBufferPointer {realBP in
imaginary.withUnsafeMutableBufferPointer {imaginaryBP in
var splitComplex = DSPDoubleSplitComplex(realp: realBP.baseAddress!, imagp: imaginaryBP.baseAddress!)
vDSP_zvphasD(&splitComplex, 1, &results, t, vDSP_Length(x.count))
}
}
return results
}