Vovich
Vovich

Reputation: 321

Fatal error when camera is initializing and back button is pressed or page navigating

I'm working on WP7/8 application with barcode scanning. And have a problem with disposing camera. Camera initialize too long, and when camera is still initializing and I press back button, I've got a fatal error:

A first chance exception of type 'System.ObjectDisposedException' occurred in Microsoft.Devices.Camera.ni.dll WinRT information: Fatal error. Disposing capture device.

Could anybody helps me how to avoid this error?

my code:

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        InitializeAndGo();

        base.OnNavigatedTo(e);
    }

    protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        disposeCamera();
    }

    private void PhotoCameraOnInitialized(object sender, CameraOperationCompletedEventArgs cameraOperationCompletedEventArgs)
    {
        _width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
        _height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

        _luminance = new PhotoCameraLuminanceSource(_width, _height);

        if (_photoCamera.IsFlashModeSupported(FlashMode.Auto))
        {
            _photoCamera.FlashMode = FlashMode.Off;
        }

        cameraInitialized = true;
        Dispatcher.BeginInvoke(() =>
        {
            FlashCheckbox.IsEnabled = true;
            if (_photoCamera.IsFlashModeSupported(FlashMode.Auto))
            {
                _photoCamera.FlashMode = FlashMode.Off;
            }
        });


        _photoCamera.Focus();
    }

     private void InitializeAndGo()
        {
             stopScan = false;
            _photoCamera = new PhotoCamera();
            _photoCamera.Initialized += PhotoCameraOnInitialized;
            _photoCamera.AutoFocusCompleted += PhotoCameraOnAutoFocusCompleted;
            viewfinderBrush.SetSource(_photoCamera);
            _previewTransform.Rotation = _photoCamera.Orientation;
            _results = new ObservableCollection<Result>();

            _barcodeReader = new BarcodeReader();
            _barcodeReader.TryHarder = true;
            _barcodeReader.AutoRotate = true;              
            _service = new MyMoviesDataService(ErrorDataService);

        }

    private void disposeCamera()
    {
        try
        {
            cameraInitialized = false;
            StopScan();
            _photoCamera.Initialized -= PhotoCameraOnInitialized;
            _photoCamera.AutoFocusCompleted -= PhotoCameraOnAutoFocusCompleted;
            _photoCamera.Dispose();
            _photoCamera = null;
        }
        catch (Exception ex)
        {
            App.ShowErrorToast(ex.Message);
        }
    }

Upvotes: 0

Views: 538

Answers (2)

Tom Lint
Tom Lint

Reputation: 499

Don't use the camera until it's been successfully initialized (You can check this in the camera's Initialized event).

Also, wrap any usages of the camera in a

try
{
    // camera code here
}
catch (ObjectDisposedException)
{
    // re-initialize the camera?
}

to handle situations like suspension, which will dispose of the camera automatically.

As for the

An exception of type 'System.ObjectDisposedException' occurred in Microsoft.Devices.Camera.ni.dll and wasn't handled before a managed/native boundary WinRT information: Fatal error. Disposing capture device.

This is something Microsoft needs to fix; I mean, how are you supposed to handle a native code exception if it isn't allowed to propagate to managed code?

Upvotes: 1

vidonism
vidonism

Reputation: 119

Where is the exception coming from (which code line / block)?

I would for starter put a try...catch around InitializeAndGo() in the OnNavigatedTo event handler. And on the whole PhotoCameraOnInitialized event handler also.

Cheers,

Upvotes: 0

Related Questions