Thalatta Thalatta - 1 year ago 241
iOS Question

Unity Vuforia Extension EXC_BAD_ACCESS with AVCaptureSession

So I am using Unity 2017.2.0f3 with the Vuforia Extension, and I imported the project in an extant iOS project following the tutorial. The extant iOS project included a view in which you can toggle between an AR view which uses Vuforia, and a native iOS AVCaptureSession which can scan barcodes. The former works without error, but as soon as I try to do

the latter feature crashes with:


within the
I tried to use NSZombie to learn more about what kind of object was trying to be sent a message, but i can only run the application on a physical device, and thus I can't learn more about the actual object it tries to send a message to which causes the crash.

My reasoning is that it may have to do with Vuforia persisting a reference to a freed VIdeoDataOutput Buffer Queue which is triggered by my creation of a new
with an
and an
. What is tricky is that I don't know how to actually ascertain this or not since it is a third party library. The only ViewController which has an Objective-C wrapper is called
but this seems to be a vestigial Unity only View Controller, since any
or breakpoints I put in that method are not called.

private func setupCamera() {
guard let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo),
let input = try? AVCaptureDeviceInput(device: device) else {

if session.canAddInput(input) {

previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.frame = self.view.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
let metadataOutput = AVCaptureMetadataOutput()

if session.canAddOutput(metadataOutput) {

metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes
} else {
print("Could not add metadata output")

Once the above method is called, even after calling the
function from the blitz tutorial
always crashes the app.

Answer Source

So basically the error I was getting was from Unity not truly exiting when in background. This is a setting which is configurable for iOS Unity exports in Unity itself. If you go to Edit> Player Settings> and then in the Inspector on the right under Other Settings there is a field called Behavior In Background. My original export had this set to Suspend and this caused the AVCaptureSession intrinsic to a Camera in Unity/Vuforia to not be probably cleaned up, in terms of its videoOutput delegate buffer, when Unity wasn't running, thus the resulting EXC_BAD_ACCESS when thinking I had Unity stopped and starting a new AVCaptureSession. So by switching Behavior In Background to Exit and then guaranteeing in my app that I had called appDelegate.stopUnity() before doing setting up input/output pairs for a new AVCaptureSession() successfully fixed the issue.
