You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When the Analyze method in BarcodeAnalyzer runs for an extended period, it throws a Cannot access a disposed object exception. This issue seems to occur due to the asynchronous nature of the method, particularly when processing images for OCR or barcode scanning.
To Reproduce
Steps to reproduce the behavior:
Ensure the method is running for a prolonged duration, ideally in a scenario with continuous image processing.
Refactor BarcodeAnalyzer:
public async void Analyze(IImageProxy proxy)
{
try
{
var mediaImage = proxy.Image;
if (mediaImage == null) return;
_lastRunTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (_lastRunTime - _lastAnalysisTime > _cameraView.ScanInterval && _cameraView.IsScanning)
{
_lastAnalysisTime = _lastRunTime;
var image = InputImage.FromMediaImage(mediaImage, proxy.ImageInfo.RotationDegrees);
OCRResult ocrFinalResult = null;
// Pass image to the scanner and have it do its thing
using (var textScanner = TextRecognition.GetClient(Xamarin.Google.MLKit.Vision.Text.Latin.TextRecognizerOptions.DefaultOptions))
{
var ocrResult = await ToAwaitableTask(textScanner.Process(image));
ocrFinalResult = OCRMethods.ProcessOCRResult(ocrResult);
}
if (!_cameraView.IsScanning)
return;
Log.Debug($"{nameof(BarcodeAnalyzer)}",
$"""
OCR: {ocrFinalResult?.AllText}
""");
}
}
catch (Java.Lang.Exception ex)
{
Log.Debug(nameof(BarcodeAnalyzer), ex.ToString());
}
catch (Exception ex)
{
Log.Debug(nameof(BarcodeAnalyzer), ex.ToString());
}
finally
{
SafeCloseImageProxy(proxy);
}
}
Observe the application for any exceptions thrown during the execution.
The Cannot access a disposed object exception should occur after some time.
Expected behavior
The Analyze method is expected to process images continuously without throwing exceptions related to disposed objects. The method should handle long-running operations and resource management efficiently.
Screenshots
If applicable, add screenshots to help explain your problem.
Smartphone (please complete the following information):
Device: Samsung A52 & A53
OS: Android 13 & 14
Version MAUI .NET8
Additional context / Solution
The issue seems to be linked to the asynchronous execution pattern in the Analyze method. A potential solution involves refactoring the method for synchronous execution, which could prevent the disposal of objects that are still in use. This refactoring would involve removing the async keyword and managing tasks and resources in a synchronous context.
Refactoring Analyze method like this seems to solve the issue:
public void Analyze(IImageProxy proxy)
{
try
{
var mediaImage = proxy.Image;
if (mediaImage == null) return;
_lastRunTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (_lastRunTime - _lastAnalysisTime > _cameraView.ScanInterval && _cameraView.IsScanning)
{
_lastAnalysisTime = _lastRunTime;
var image = InputImage.FromMediaImage(mediaImage, proxy.ImageInfo.RotationDegrees);
List<BarcodeResult> barcodeFinalResult = null;
OCRResult ocrFinalResult = null;
// Pass image to the scanner and have it do its thing
if (_cameraView.IsOCR)
{
using (var textScanner = TextRecognition.GetClient(Xamarin.Google.MLKit.Vision.Text.Latin.TextRecognizerOptions.DefaultOptions))
{
var ocrTask = ToAwaitableTask(textScanner.Process(image));
ocrTask.Wait();
ocrFinalResult = OCRMethods.ProcessOCRResult(ocrTask.Result);
}
}
else
{
var barcodeTask = ToAwaitableTask(_barcodeScanner.Process(image));
barcodeTask.Wait();
barcodeFinalResult = Methods.ProcessBarcodeResult(barcodeTask.Result);
if (barcodeFinalResult == null || _cameraView == null) return;
}
if (!_cameraView.IsScanning)
return;
var imageData = Array.Empty<byte>();
if (_cameraView.ReturnBarcodeImage)
{
imageData = NV21toJPEG(YUV_420_888toNV21(mediaImage), mediaImage.Width, mediaImage.Height);
imageData = RotateJpeg(imageData, GetImageRotationCorrectionDegrees());
}
_cameraView.IsScanning = false;
_cameraView.TriggerOnDetected(ocrFinalResult, barcodeFinalResult, imageData);
if (_cameraView.VibrationOnDetected)
Vibration.Vibrate(200);
}
}
catch (Java.Lang.Exception ex)
{
Log.Debug(nameof(BarcodeAnalyzer), ex.ToString());
}
catch (Exception ex)
{
Log.Debug(nameof(BarcodeAnalyzer), ex.ToString());
}
finally
{
SafeCloseImageProxy(proxy);
}
}
The text was updated successfully, but these errors were encountered:
Describe the bug
When the
Analyze
method inBarcodeAnalyzer
runs for an extended period, it throws a Cannot access a disposed object exception. This issue seems to occur due to the asynchronous nature of the method, particularly when processing images for OCR or barcode scanning.To Reproduce
Steps to reproduce the behavior:
Ensure the method is running for a prolonged duration, ideally in a scenario with continuous image processing.
SafeCloseImageProxy
catch block:Cannot access a disposed object
exception should occur after some time.Expected behavior
The Analyze method is expected to process images continuously without throwing exceptions related to disposed objects. The method should handle long-running operations and resource management efficiently.
Screenshots
If applicable, add screenshots to help explain your problem.
Smartphone (please complete the following information):
Additional context / Solution
The issue seems to be linked to the asynchronous execution pattern in the Analyze method. A potential solution involves refactoring the method for synchronous execution, which could prevent the disposal of objects that are still in use. This refactoring would involve removing the async keyword and managing tasks and resources in a synchronous context.
Refactoring Analyze method like this seems to solve the issue:
The text was updated successfully, but these errors were encountered: