[swift-users] Still having trouble with C interop (passing buffers)

Rick Mann rmann at latencyzero.com
Tue Apr 25 16:57:20 CDT 2017


I'm trying to pass a Data of allocated size to a C function for it to fill in:


lib_example_call(_ params: UnsafePointer<lib_call_params_t>!, _ data: UnsafeMutableRawPointer!)

...
{
    self.dataBuffer = Data(capacity: BufferSizeConstant)

    var params = lib_call_params_t();
    params.data_capacity = BufferSizeConstant;

    self.dataBuffer?.withUnsafeMutableBytes
    { (inBuffer) -> Void in
        lib_example_call(&params, inBuffer)
    }
}

I later get called back by the library with a size value of the actual data it got. self.dataBuffer is a var. I set self.dataBuffer?.count = result size, which is a reasonable value.

Unfortunately, the resulting buffer is all zeros. The data generated by the call is definitely not all zero, and a C example program using the same library works correctly.

So, I think there's something wrong in the way I'm making the call.

Can anyone please enlighten me? Thanks!

-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list