On 15 Nov 2017, at 04:16, Rick Mann via swift-users <swift-users@swift.org> 
wrote:

> Is UnsafeMutablePointer<> not memory managed?

It is not.  Specifically, the code you posted creates a copy of the data and 
then never destroys it.

If I were in your shoes I’d construct a `Data` value from the unsafe pointer 
and then pass that around.

let source = Data(bytes: inSourceBuffer, count: inSourceBufferLength)
self.queue.async {
    let size = source.count
    source.withUnsafeBytes { (p: UnsafePointer<UInt8>) in
        self.foo(data: p, length: size)
    }
}

The main drawback to this is that you have to jump through the hoops to access 
the data unsafely.  It might be easier to recast your consumer (the `foo(…)` 
method) in terms of `Data`.  That’s what I generally do when I work with 
foreign APIs like this, that is, keep the data in ‘Swift space’ and only deal 
with foreign types at the boundaries.

Whether that makes sense here really depends on the specifics of your program.  
For example, if your program has lots of this boundary code, it might be nicer 
to just stick with the foreign type.  Or build a specific wrapper that makes it 
easier to do this conversion.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to