I have seen other questions dealing with “Capture of … with non-sendable type ‘…’ in an isolated closure” error, but none of them talk about autoreleasepool
.
The relevant code:
Error appears in this part:
Self.workQueue.async {
autoreleasepool {
// Error in the following line:
self.doWork(pixelBuffer: pixelBuffer, completion: completion)
}
}
The error verbatim is:
Capture of ‘pixelBuffer’ with non-sendable type ‘CVPixelBuffer’ (aka ‘CVBuffer’) in an isolated closure; this is an error in the Swift 6 language mode
Class ‘CVBuffer’ does not conform to the ‘Sendable’ protocol (CoreVideo.CVBuffer)
The workqueue is defined as:
private static let workQueue = DispatchQueue(
label: "...",
qos: .userInitiated,
autoreleaseFrequency: .workItem, // switching from default .never
target: .global(qos: .userInitiated)
)
and doWork
is defined as a private extension to the same class with signature
func doWork(pixelBuffer: CVPixelBuffer, completion: @escaping (SomeResult?, Error?) -> Void)
I can resolve the error if I remove autoreleasepool
, i.e. this won’t produce any error:
Self.workQueue.async {
self.doWork(pixelBuffer: pixelBuffer, completion: completion)
}
But the autoreleasepool
was introduced as a fix for memory leak, related to doWork
processing of the frames.
So now I am trying to understand what changed, and how to adjust to new requirements.
To be clear I am not looking for solution to this issue (it may require bigger refactoring, than fits one question). I am looking for explanation / guidance on:
- What this error in conjunction with
autoreleasepool
means - What would be a correct way to use
CVPixelBuffer
/CVBuffer
withautoreleasepool
?
Thanks in advance.