fix(ios): CVPixelBuffer race condition crash in captureOutput#1676
Closed
harSofja wants to merge 1 commit intojuliansteenbakker:developfrom
Hidden character warning
The head ref may contain hidden characters: "fix\u200b/cvpixelbuffer\u200b-race\u200b-condition"
Closed
fix(ios): CVPixelBuffer race condition crash in captureOutput#1676harSofja wants to merge 1 commit intojuliansteenbakker:developfrom
harSofja wants to merge 1 commit intojuliansteenbakker:developfrom
Conversation
Capture a strong reference to latestBuffer before dispatching to background thread. Fixes juliansteenbakker#1545
navaronbracke
approved these changes
Mar 23, 2026
Collaborator
navaronbracke
left a comment
There was a problem hiding this comment.
Good catch! Yeah, chances are that there's a threading issue, since there's stuff running on the global dispatch queue.
| } | ||
|
|
||
| guard let buffer = self.latestBuffer else { | ||
| let bufferToProcess = latestBuffer |
Collaborator
There was a problem hiding this comment.
Is the formatting off here? (relative to the surrounding code)
Collaborator
|
@harSofja I see that Github provides me with a warning on the pull request? Does the branch need to be renamed? Also, can you add an entry in the changelog + bump the patch version in the pubspec? |
Author
|
Since the GitHub web editor is getting messy with hidden characters I will close this PR and create a clean one. I will also look at the formatting and add an entry in the change log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a race condition in
captureOutputwhereself.latestBufferis read on a background thread (DispatchQueue.global(qos: .userInitiated)) after being deallocated or overwritten on the main thread by the next camera frame orreleaseCamera().The fix captures a strong reference to the buffer before dispatching to the background queue, ensuring the
CVPixelBufferis retained for the duration of the background processing.Crash Stack Trace (from Xcode Organizer)
Thread 1 Crashed:
0 CoreVideo CVPixelBufferGetWidth + 28
1 VideoToolbox VTCreateCGImageFromCVPixelBuffer
2 mobile_scanner closure #1 in MobileScannerPlugin.captureOutput(:didOutput:from:)
3 mobile_scanner MobileScannerPlugin.captureOutput(:didOutput:from:) (partial)
4 libdispatch _dispatch_call_block_and_release
5 libdispatch _dispatch_client_callout
Environment
Related Issue
Fixes #1545