> On Nov 15, 2017, at 00:48 , Quinn The Eskimo! via swift-users 
> <swift-users@swift.org> wrote:
> 
> 
> 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.

Hmm. So it should leak, not crash. I wonder what I'm doing wrong.

> 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.

Thanks. I have to call this code from Objective-C, passing a pointer and length 
(which is how I get the raw data). I could create CGImages from that, and I 
might, since eventually the consumer will be implemented in Metal 2. But right 
now, I just have to get back to the raw data to do the processing, so it seemed 
like extra work to create a CGImage.


-- 
Rick Mann
rm...@latencyzero.com


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

Reply via email to