Skip to content

fix(ios): CVPixelBuffer race condition crash in captureOutput#1676

Closed
harSofja wants to merge 1 commit intojuliansteenbakker:developfrom
harSofja:fix​/cvpixelbuffer​-race​-condition

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#1676
harSofja wants to merge 1 commit intojuliansteenbakker:developfrom
harSofja:fix​/cvpixelbuffer​-race​-condition

Conversation

@harSofja
Copy link
Copy Markdown

Summary

Fixes a race condition in captureOutput where self.latestBuffer is read on a background thread (DispatchQueue.global(qos: .userInitiated)) after being deallocated or overwritten on the main thread by the next camera frame or releaseCamera().

The fix captures a strong reference to the buffer before dispatching to the background queue, ensuring the CVPixelBuffer is 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

  • mobile_scanner: 7.2.0
  • iOS: 26.0.1
  • Devices affected: 54 (iPhone 16 Pro Max, iPhone 15 Pro Max, and others)
  • Architecture: ARM-64
  • Distribution: App Store

Related Issue

Fixes #1545

Capture a strong reference to latest​Buffer before dispatching to background thread. ​Fixes juliansteenbakker#1545
@harSofja harSofja changed the title [​Apple] ​Fix ​CVPixel​Buffer race condition crash in capture​Output fix(ios): CVPixelBuffer race condition crash in captureOutput Mar 18, 2026
Copy link
Copy Markdown
Collaborator

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the formatting off here? (relative to the surrounding code)

@navaronbracke
Copy link
Copy Markdown
Collaborator

navaronbracke commented Mar 23, 2026

@harSofja I see that Github provides me with a warning on the pull request?

The head ref may contain hidden characters: "fix\u200b/cvpixelbuffer\u200b-race\u200b-condition"

Does the branch need to be renamed?

Also, can you add an entry in the changelog + bump the patch version in the pubspec?

@harSofja
Copy link
Copy Markdown
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

@harSofja harSofja closed this Mar 24, 2026
@harSofja harSofja deleted the fix​/cvpixelbuffer​-race​-condition branch March 24, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS] EXC_BREAKPOINT crash in captureOutput when VTCreateCGImageFromCVPixelBuffer fails

2 participants