Skip to content

Instantly share code, notes, and snippets.

@elfenlaid
Last active January 6, 2021 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfenlaid/aa9ab4ccaebc39b61bbd932bc307b2b0 to your computer and use it in GitHub Desktop.
Save elfenlaid/aa9ab4ccaebc39b61bbd932bc307b2b0 to your computer and use it in GitHub Desktop.
Calling Core Foundation functions with pointer arguments

Calling Core Foundation functions with pointer arguments

Thus far, I've encountered two approaches to manage a Create/Copy memory:

  • unsafe_pointer
  • plain_ref

At this point, it's hard to pick the right path. Is it safe to assume their equivalence?

@nnnnnnnn kindly has confirmed viability of both methods. Though a plain_ref is somewhat superior in ergonomics.

References

Examples

  • Example manages single pixel buffer via plain ref
  • Example recyles pixel buffers via plain ref approach
  • Example recycles pixel buffers via unmanaged pointer
let pixelBufferPool: CVPixelBufferPool = // ...
unsafe_pointer: do {
let pixelBufferPointer = UnsafeMutablePointer<CVPixelBuffer?>.allocate(capacity: 1)
defer {
pixelBufferPointer.deinitialize(count: 1)
pixelBufferPointer.deallocate()
}
CVPixelBufferPoolCreatePixelBuffer(
nil,
pixelBufferPool,
pixelBufferPointer
)
}
plain_ref: do {
var pxbuffer: CVPixelBuffer?
CVPixelBufferPoolCreatePixelBuffer(
nil,
pixelBufferPool,
&pxbuffer
)
pxbuffer = nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment