[swift-users] Making a copy of an UnsafePointer<UInt8>

Rick Mann rmann at latencyzero.com
Tue Nov 14 22:15:58 CST 2017


My code gets some data from an OpenCV cv::Mat object, and passes it to some Swift code that takes it via an UnsafePointer<UInt8> parameter. This works fine until I want to make the call asynchronous (using a DispatchQueue), I think because the underlying cv::Mat gets deallocated by the caller before the async method completes.

So I want to make a copy of it to hold onto during the async operation. I tried this:

    let copy = UnsafeMutablePointer<UInt8>.allocate(capacity: inSourceBufferLength)
    copy.initialize(from: inSourceBuffer, count: inSourceBufferLength)
		
    self.queue.async
    {
     	self.foo(data: copy, length: inSourceBufferLength)
    }

But that doesn't seem to be working. Is UnsafeMutablePointer<> not memory managed?

I'd also like to keep immutable semantics throughout, but I can forego that if it makes things really messy.

-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list