var params = lib_call_params_t();
params.data_capacity = BufferSizeConstant;
self.dataBuffer?.withUnsafeMutableBytes
{ (inBuffer) -> Void in
lib_example_call(¶ms, 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.
If you want to create a buffer with a given count you should use Data(count: Int), the method you are using just reserves a given capacity. (from what I can tell from your code that is likely what you really want)
···
On Apr 25, 2017, at 2:57 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
var params = lib_call_params_t();
params.data_capacity = BufferSizeConstant;
self.dataBuffer?.withUnsafeMutableBytes
{ (inBuffer) -> Void in
lib_example_call(¶ms, 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.
Yup, that seems to be it. I would have thought that init(capacity:) should work, too (it could defer allocation until, say, the call withUnsafeMutableBytes()), and just not zero the contents (the init(count:) call zeroes the contents).
···
On Apr 25, 2017, at 15:20 , Philippe Hausler <phausler@apple.com> wrote:
On Apr 25, 2017, at 2:57 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:
I'm trying to pass a Data of allocated size to a C function for it to fill in:
If you want to create a buffer with a given count you should use Data(count: Int), the method you are using just reserves a given capacity. (from what I can tell from your code that is likely what you really want)
var params = lib_call_params_t();
params.data_capacity = BufferSizeConstant;
self.dataBuffer?.withUnsafeMutableBytes
{ (inBuffer) -> Void in
lib_example_call(¶ms, 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.