Re: [swift-users] Making a copy of an UnsafePointer<UInt8>
Re: [swift-users] Making a copy of an UnsafePointer<UInt8>
- Subject: Re: [swift-users] Making a copy of an UnsafePointer<UInt8>
- From: Rick Mann <email@hidden>
- Date: Wed, 15 Nov 2017 16:03:18 -0800
Ah, never mind. I was indexing well past the end of the buffer. D'oh.
> On Nov 15, 2017, at 00:48 , Quinn The Eskimo! via swift-users
> <email@hidden> wrote:
>
>
> On 15 Nov 2017, at 04:16, Rick Mann via swift-users <email@hidden>
> 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
> email@hidden
> https://lists.swift.org/mailman/listinfo/swift-users
--
Rick Mann
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden