Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 703 forks source link

App restart while using ZXing Barcode Library #372

Closed duma23 closed 4 years ago

duma23 commented 8 years ago

Hi,

I have problem with ZXing library, I have tried with zxing.net.mobile and zxing.net.mobile.forms but the result is the same, app restart. I don't get any exception, and I get this with simple Scan and with ScanContinuously.

These are last few lines from output:

07-21 16:16:36.320 I/dalvikvm-heap( 1349): Grow heap (frag case) to 13.727MB for 460816-byte allocation 07-21 16:16:36.360 D/dalvikvm( 1349): GC_FOR_ALLOC freed 0K, 14% free 12922K/14855K, paused 38ms, total 38ms 07-21 16:16:36.420 D/dalvikvm( 1349): GC_FOR_ALLOC freed 900K, 20% free 12023K/14855K, paused 26ms, total 26ms 07-21 16:16:36.495 D/dalvikvm( 1349): GC_CONCURRENT freed 900K, 20% free 12024K/14855K, paused 5ms+5ms, total 63ms 07-21 16:16:36.495 D/dalvikvm( 1349): WAIT_FOR_CONCURRENT_GC blocked 32ms 07-21 16:16:36.605 D/dalvikvm( 1349): GC_CONCURRENT freed 450K, 17% free 12475K/14855K, paused 2ms+7ms, total 74ms 07-21 16:16:36.605 D/dalvikvm( 1349): WAIT_FOR_CONCURRENT_GC blocked 41ms 07-21 16:16:36.675 D/dalvikvm( 1349): GC_FOR_ALLOC freed 900K, 20% free 12025K/14855K, paused 47ms, total 47ms 07-21 16:16:36.730 D/dalvikvm( 1349): GC_FOR_ALLOC freed 452K, 19% free 12035K/14855K, paused 36ms, total 36ms 07-21 16:16:36.790 D/dalvikvm( 1349): GC_CONCURRENT freed 900K, 19% free 12036K/14855K, paused 4ms+2ms, total 28ms 07-21 16:16:36.870 D/dalvikvm( 1349): GC_CONCURRENT freed 452K, 16% free 12495K/14855K, paused 12ms+3ms, total 40ms 07-21 16:16:36.870 D/dalvikvm( 1349): WAIT_FOR_CONCURRENT_GC blocked 8ms 07-21 16:16:36.870 D/dalvikvm( 1349): WAIT_FOR_CONCURRENT_GC blocked 23ms 07-21 16:16:36.940 D/dalvikvm( 1349): GC_FOR_ALLOC freed 900K, 19% free 12046K/14855K, paused 43ms, total 43ms 07-21 16:16:37.005 D/dalvikvm( 1349): GC_CONCURRENT freed 900K, 19% free 12047K/14855K, paused 2ms+4ms, total 37ms 07-21 16:16:37.005 D/dalvikvm( 1349): WAIT_FOR_CONCURRENT_GC blocked 3ms

Any idea?

Thanks!

jseter commented 8 years ago

I got an exception or two from the java layers when using the library in places that seem unrelated, such as after scanning and navigating backwards in my application. One thing i noticed while reviewing the source code of the ZXing.Net.Mobile application is that the Scan method uses Task.Factory.StartNew As I understand it, the UI layer of Android is not thread safe, therefore StartActivity and some other calls used in this action are running in the threadpool instead of the main thread and potentially introducing issues. ex. http://stackoverflow.com/questions/6274653/how-to-start-an-activity-from-a-thread-class-in-android

rocuh commented 8 years ago

I had the same issue, for me it was to do with the environment.txt settings on android i was using for the GC. I reduced the nursery-size and the soft-heap-limit settings to something more reasonable and then no zxing abort.

wammy21 commented 6 years ago

Hate to bump a thread this old but I am experiencing the same issue and this is the only relevant thread/issue I have found.

App appears to be resetting (on Android, I have not tested iOS) after the barcode is scanned, here is the debug log starting from when my scan button is clicked to when the app resets:

03-15 09:27:10.434 D/ZXing.Net.Mobile(32027): ZXingScannerFragment->OnResume exit
03-15 09:27:10.524 D/ZXing.Net.Mobile(32027): Checking android.permission.CAMERA...
Loaded assembly: System.Diagnostics.Debug.dll [External]
03-15 09:27:10.546 D/ZXing.Net.Mobile(32027): Checking Number of cameras...
03-15 09:27:10.554 D/ZXing.Net.Mobile(32027): Found 3 cameras...
03-15 09:27:10.557 D/ZXing.Net.Mobile(32027): Found Back Camera, opening...
03-15 09:27:11.265 D/ZXing.Net.Mobile(32027): Selected Resolution: 880x720
03-15 09:27:11.299 D/ZXing.Net.Mobile(32027): Changing Camera Orientation to: 90
03-15 09:27:11.916 D/ZXing.Net.Mobile(32027): Selected Resolution: 880x720
03-15 09:27:11.926 D/ZXing.Net.Mobile(32027): Changing Camera Orientation to: 90
03-15 09:27:11.932 I/Choreographer(32027): Skipped 86 frames!  The application may be doing too much work on its main thread.
03-15 09:27:12.119 D/Camera  (32027): app passed NULL surface
03-15 09:27:12.585 D/ZXing.Net.Mobile(32027): ZXingScannerFragment->OnResume exit
03-15 09:27:12.595 D/ZXing.Net.Mobile(32027): ZXingScannerFragment->OnResume exit
03-15 09:27:12.627 I/Choreographer(32027): Skipped 33 frames!  The application may be doing too much work on its main thread.
03-15 09:27:12.661 D/ZXing.Net.Mobile(32027): Checking android.permission.CAMERA...
03-15 09:27:12.665 D/ZXing.Net.Mobile(32027): Checking Number of cameras...
03-15 09:27:12.666 D/ZXing.Net.Mobile(32027): Found 3 cameras...
03-15 09:27:12.667 D/ZXing.Net.Mobile(32027): Found Back Camera, opening...
03-15 09:27:12.835 D/ZXing.Net.Mobile(32027): Selected Resolution: 880x720
03-15 09:27:12.841 D/ZXing.Net.Mobile(32027): Changing Camera Orientation to: 90
03-15 09:27:13.277 D/ZXing.Net.Mobile(32027): Selected Resolution: 880x720
03-15 09:27:13.286 D/ZXing.Net.Mobile(32027): Changing Camera Orientation to: 90
03-15 09:27:13.295 I/Choreographer(32027): Skipped 39 frames!  The application may be doing too much work on its main thread.
03-15 09:27:13.404 I/ViewRootImpl(32027): ViewRoot's Touch Event : ACTION_DOWN
03-15 09:27:13.406 I/ViewRootImpl(32027): ViewRoot's Touch Event : ACTION_CANCEL
03-15 09:27:13.433 I/Timeline(32027): Timeline: Activity_idle id: android.os.BinderProxy@2ac3a7e time:662459234
03-15 09:27:13.433 I/Timeline(32027): Timeline: Activity_idle id: android.os.BinderProxy@2ac3a7e time:662459235
Loaded assembly: System.ServiceModel.Internals.dll [External]
03-15 09:27:15.314 I/art     (32027): Starting a blocking GC Explicit
03-15 09:27:15.371 I/art     (32027): Explicit concurrent mark sweep GC freed 34126(2MB) AllocSpace objects, 8(4MB) LOS objects, 40% free, 23MB/39MB, paused 519us total 56.904ms
03-15 09:27:16.855 I/art     (32027): Starting a blocking GC Explicit
03-15 09:27:16.901 I/art     (32027): Explicit concurrent mark sweep GC freed 11968(981KB) AllocSpace objects, 17(2MB) LOS objects, 40% free, 19MB/33MB, paused 633us total 45.573ms
03-15 09:27:18.579 I/art     (32027): Starting a blocking GC Explicit
03-15 09:27:18.615 I/art     (32027): Explicit concurrent mark sweep GC freed 3855(265KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 19MB/32MB, paused 600us total 35.721ms
03-15 09:27:20.162 I/art     (32027): Starting a blocking GC Explicit
03-15 09:27:20.196 I/art     (32027): Explicit concurrent mark sweep GC freed 2056(188KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 19MB/32MB, paused 489us total 33.895ms
Loaded assembly: System.Text.Encoding.dll [External]
03-15 09:27:20.895 D/ZXing.Net.Mobile(32027): Barcode Found: 65c15bd5-63d5-40e6-bdfc-3ebbc93595e2
Deal UUID: 65c15bd5-63d5-40e6-bdfc-3ebbc93595e2
03-15 09:27:20.913 I/mono-stdout(32027): Code UUID: 65c15bd5-63d5-40e6-bdfc-3ebbc93595e2

Here is my scan settings /method

private async void ScanButton_Clicked(object sender, EventArgs e)
{
    var options = new MobileBarcodeScanningOptions
    {
        PossibleFormats = new List<ZXing.BarcodeFormat>() {
            ZXing.BarcodeFormat.QR_CODE
         }
    };

     var scanner = new MobileBarcodeScanner();
     var result = await scanner.Scan(options);
#if DEBUG
     Console.WriteLine("Code UUID: " + result.Text);
#endif
}

This is my MainActivity.OnCreate:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    global::Xamarin.Forms.Forms.Init(this, bundle);

    Xamarin.FormsMaps.Init(this, bundle);

    ZXing.Net.Mobile.Forms.Android.Platform.Init();

    LoadApplication(new App());

    IsPlayServicesAvailable();

    MobileBarcodeScanner.Initialize(Application);
}

I tried to look up the solution by @roceh but not sure what that means? I found this info, and set up the GC settings as such:

MONO_GC_PARAMS=bridge-implementation=tarjan,nursery-size=128m,soft-heap-limit=512m

Any help is appreciated

wammy21 commented 6 years ago

So after some more digging and checking into what the conditions are when the app restarts I have trace it to my MainActivity's OnResume method, a condition caused an event to be re-raised that re initiated the whole app.

Redth commented 4 years ago

Thanks for reporting this issue! Unforunately it took me way too long to respond 😭. Sorry, I apologize! Recently the source code for this project was completely refactored to modernize it. Many PR's were included in this effort, and many bugs were hopefully fixed. Please try out the latest 3.x series of NuGet packages (currently in prerelease). To try and make the project more maintainable in my spare time going forward, I've decided to close all existing issues to start with a clean slate. If you're still experiencing this issue on the newest version, please open a new issue with as much detail as possible. Thank you for your patience and understanding! Happy scanning!