On Fri, Jul 29, 2016 at 12:55 AM, James Campbell <ja...@supmenow.com> wrote:
> So this:
>
> if let data = someArrayGeneratingFunction() {
>   cFunction(UnsafeMutablePointer(data))
> }
>
> Has issues with the array passed to c getting corrupted, but this doesn't:
>
> let data = someArrayGeneratingFunction()
>
> if let data = data {
>   cFunction(UnsafeMutablePointer(data))
> }

Neither piece of code is guaranteed to work.  (You are just getting
lucky that the second one happens to work.)  Array-to-pointer
conversion only extends the lifetime of the array until the immediate
function call returns.  So after UnsafeMutablePointer(data) returns,
the array can be freed.

Use someArrayGeneratingFunction.withUnsafeMutableBuffer { ... } instead.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <griboz...@gmail.com>*/
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to