CariusLars / ar_flutter_plugin

Flutter Plugin for AR (Augmented Reality) - Supports ARKit on iOS and ARCore on Android devices
MIT License
330 stars 238 forks source link

App crashes when it is brought back to foreground when it's running in background. #38

Open lecanhhiep opened 2 years ago

lecanhhiep commented 2 years ago

Hi @CariusLars ,

We're facing the crash issue on Android 7.1 (Note 8). App is always crash when it run at background and bring back to foreground (onPause -> onResume) and throw this exception:

E/AndroidRuntime(18270): java.lang.NullPointerException: throw with null exception E/AndroidRuntime(18270): at com.google.ar.sceneform.utilities.Preconditions.checkNotNull(SourceFile:3) E/AndroidRuntime(18270): at com.google.ar.sceneform.Scene.setUseHdrLightEstimate(SourceFile:101) E/AndroidRuntime(18270): at com.google.ar.sceneform.ArSceneView.updateNormalLightEstimate(SourceFile:204) E/AndroidRuntime(18270): at com.google.ar.sceneform.ArSceneView.updateLightEstimate(SourceFile:136) E/AndroidRuntime(18270): at com.google.ar.sceneform.ArSceneView.onBeginFrame(SourceFile:124) E/AndroidRuntime(18270): at com.google.ar.sceneform.SceneView.doFrameNoRepost(SourceFile:90) E/AndroidRuntime(18270): at com.google.ar.sceneform.SceneView.doFrame(SourceFile:86) E/AndroidRuntime(18270): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:928) E/AndroidRuntime(18270): at android.view.Choreographer.doCallbacks(Choreographer.java:705) E/AndroidRuntime(18270): at android.view.Choreographer.doFrame(Choreographer.java:637) E/AndroidRuntime(18270): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:916) E/AndroidRuntime(18270): at android.os.Handler.handleCallback(Handler.java:751) E/AndroidRuntime(18270): at android.os.Handler.dispatchMessage(Handler.java:95) E/AndroidRuntime(18270): at android.os.Looper.loop(Looper.java:154) E/AndroidRuntime(18270): at android.app.ActivityThread.main(ActivityThread.java:6823) E/AndroidRuntime(18270): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(18270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563) E/AndroidRuntime(18270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

I have tried by adding:

@override void dispose() { try { print("Removeing node"); if (this.localObjectNode != null) { this.arObjectManager?.removeNode(this.localObjectNode!); this.localObjectNode = null; } } catch(err, stackstrace) { print(stackstrace); }

super.dispose(); }

when close the AR popup dialog, but when I leave the app running at background and bring it back to foreground, the above exception is still happening and app crash.

Could you please give us some advice to fix it?

Thanks,

CariusLars commented 2 years ago

Hey @lecanhhiep and sorry for the late answer! Does this happen with the example application of the plugin or with your own app that utilizes the plugin? I've just tried with the example application on my Android9 device and I can but the app into the background and back to the foreground without it crashing, so it would be good to have some more information on what system and what code you use as well as the exact scenario in which the crash happens. To debug this issue, I'd suggest placing some breakpoints into the onResume() function (line 310 onwards) of the AndroidARView and skipping through them to see which line causes the crash. Let me know if you find which object or line causes the crash and I can look into it some more! Best regards Lars

naveenbharadwaj19 commented 2 years ago

@CariusLars I'm facing the same issue . But for me crash it's happening when i open ar camera second time.(from background to foreground) Tested on both emulator and physical device (one plus 6)

Output log:

E/ACameraDevice(14182): Camera device 0 end configure failed: Status(-8, EX_SERVICE_SPECIFIC): '10: endConfigure:510: Camera 0: Error configuring streams: Function not implemented (-38)'
E/ACameraDevice(14182): Fail to create new session. cannot configure streams
I/native  (14182): I1027 14:42:24.403587   14512 logger.h:28] CameraMetrics: CreateCaptureSession: 53.512719ms
I/native  (14182): I1027 14:42:24.403664   14512 logger.h:28] DataSourceMetrics: kFirstGlCallback: 1.316786514s
I/native  (14182): I1027 14:42:23.170288   14508 cameras.cc:1387] Camera 0 changed state CLOSED->OPEN; Camera device opened successfully. i=10
I/native  (14182): I1027 14:42:23.170372   14508 cameras.cc:1387] Camera 0 changed state OPEN->RECOVERING; Attempting recovery during capture session creation from:  Code=-10000; Message=UNKNOWN: ACameraDevice_createCaptureSession ACAMERA_ERROR_UNKNOWN [type.googleapis.com/util.ErrorSpacePayload='CameraStatusErrorSpaceClass::ACAMERA_ERROR_UNKNOWN'] i=11
I/native  (14182): I1027 14:42:23.171008   14509 logger.h:28] CameraMetrics: CloseCamera: 940.064us
W/com.cart.app(14182): No such thread for suspend: 0xbf6313c0:C2N-dev-looper
I/native  (14182): I1027 14:42:24.418578   14599 logger.h:28] CameraMetrics: OpenCamera: 56.320723ms
D/ACameraDevice(14182): Device error received, code 0, frame number 0, request ID -1, subseq ID 0
E/ACameraDevice(14182): Camera device 0 failed to create stream: Status(-8, EX_SERVICE_SPECIFIC): '4: checkPidStatus:2264: The camera device has been disconnected'
E/ACameraDevice(14182): Fail to create new session. cannot configure streams
I/native  (14182): I1027 14:42:24.466899   14599 logger.h:28] CameraMetrics: CreateCaptureSession: 47.697629ms
I/native  (14182): I1027 14:42:23.343167   14595 cameras.cc:1387] Camera 0 changed state CLOSED->OPEN; Camera device opened successfully. i=5
I/native  (14182): I1027 14:42:23.343200   14595 cameras.cc:1387] Camera 0 changed state OPEN->RECOVERING; Attempting recovery during capture session creation from:  Code=-10000; Message=UNKNOWN: ACameraDevice_createCaptureSession ACAMERA_ERROR_UNKNOWN [type.googleapis.com/util.ErrorSpacePayload='CameraStatusErrorSpaceClass::ACAMERA_ERROR_UNKNOWN'] i=6
I/native  (14182): I1027 14:42:23.343943   14596 logger.h:28] CameraMetrics: CloseCamera: 888.867us
I/native  (14182): I1027 14:42:23.250639   14509 logger.h:28] CameraMetrics: OpenCamera: 79.500887ms
D/ACameraDevice(14182): Device error received, code 0, frame number 0, request ID -1, subseq ID 0

flutter doctor -v:

[√] Flutter (Channel stable, 2.5.0, on Microsoft Windows [Version 10.0.19043.1288], locale en-IN)
    • Flutter version 2.5.0 at D:\OTHER FILES D\CS ALL FOLD\Flutter main sdk\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4cc385b4b8 (7 weeks ago), 2021-09-07 23:01:49 -0700
    • Engine revision f0826da7ef
    • Dart version 2.14.0

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at D:\Android_SDK
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = D:\Android_SDK
    • Java binary at: D:\Android studio main\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2020.3)
    • Android Studio at D:\Android studio main
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.53.2)
    • VS Code at C:\Users\bhara\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.20.0

[√] Connected device (3 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 94.0.4606.81    
    • Edge (web)              • edge          • web-javascript • Microsoft Edge 95.0.1020.30   

• No issues found!
CariusLars commented 2 years ago

@naveenbharadwaj19 is the app you use public so I can try it on my device as well and potentially debug? I'm trying to better understand which line in the onResume() function causes the error, maybe you could also place some breakpoints there and go through it step by step to see which function call causes the crash?

Also, with "from background to foreground", do you mean navigating somewhere else in your android phone or staying in the Flutter app but opening another view and then going back to camera view?

naveenbharadwaj19 commented 2 years ago
Minimal code ```dart import 'package:ar_flutter_plugin/managers/ar_location_manager.dart'; import 'package:ar_flutter_plugin/managers/ar_session_manager.dart'; import 'package:ar_flutter_plugin/managers/ar_object_manager.dart'; import 'package:ar_flutter_plugin/managers/ar_anchor_manager.dart'; import 'package:ar_flutter_plugin/models/ar_anchor.dart'; import 'package:flutter/material.dart'; import 'package:ar_flutter_plugin/ar_flutter_plugin.dart'; import 'package:ar_flutter_plugin/datatypes/config_planedetection.dart'; import 'package:ar_flutter_plugin/datatypes/node_types.dart'; import 'package:ar_flutter_plugin/datatypes/hittest_result_types.dart'; import 'package:ar_flutter_plugin/models/ar_node.dart'; import 'package:ar_flutter_plugin/models/ar_hittest_result.dart'; import 'package:vector_math/vector_math_64.dart'; class ArScreen extends StatefulWidget { static const routeName = "ar-screen"; // const ArScreen({Key? key}) : super(key: key); @override _ArScreenState createState() => _ArScreenState(); } class _ArScreenState extends State { ARSessionManager? arSessionManager; ARObjectManager? arObjectManager; ARAnchorManager? arAnchorManager; List nodes = []; List anchors = []; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Anchors & Objects on Planes'), ), body: Stack( children: [ ARView( onARViewCreated: onARViewCreated, planeDetectionConfig: PlaneDetectionConfig.horizontal, ), Align( alignment: FractionalOffset.bottomCenter, child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( onPressed: onRemoveEverything, child: const Text("Remove Everything")), ], ), ), ], ), ); } void onARViewCreated( ARSessionManager arSessionManager, ARObjectManager arObjectManager, ARAnchorManager arAnchorManager, ARLocationManager arLocationManager) { this.arSessionManager = arSessionManager; this.arObjectManager = arObjectManager; this.arAnchorManager = arAnchorManager; this.arSessionManager!.onInitialize( showFeaturePoints: false, showPlanes: true, showWorldOrigin: false, ); this.arObjectManager!.onInitialize(); this.arSessionManager!.onPlaneOrPointTap = onPlaneOrPointTapped; this.arObjectManager!.onNodeTap = onNodeTapped; } Future onRemoveEverything() async { /*nodes.forEach((node) { this.arObjectManager.removeNode(node); });*/ for (var anchor in anchors) { arAnchorManager!.removeAnchor(anchor); } anchors = []; } Future onNodeTapped(List nodes) async { var number = nodes.length; arSessionManager!.onError("Tapped $number node(s)"); } Future onPlaneOrPointTapped( List hitTestResults) async { var singleHitTestResult = hitTestResults.firstWhere( (hitTestResult) => hitTestResult.type == ARHitTestResultType.plane); var newAnchor = ARPlaneAnchor(transformation: singleHitTestResult.worldTransform); bool didAddAnchor = await arAnchorManager!.addAnchor(newAnchor) as bool; if (didAddAnchor) { anchors.add(newAnchor); var newNode = ARNode( name: "ToyCar", type: NodeType.webGLB, uri: "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/ToyCar/glTF-Binary/ToyCar.glb", scale: Vector3(10, 10, 10), position: Vector3(0.0, 0.0, 0.0), rotation: Vector4(1.0, 0.0, 0.0, 0.0), ); bool didAddNodeToAnchor = await arObjectManager! .addNode(newNode, planeAnchor: newAnchor) as bool; if (didAddNodeToAnchor) { nodes.add(newNode); } else { arSessionManager!.onError("Adding Node to Anchor failed"); } } else { arSessionManager!.onError("Adding Anchor failed"); } } } ```

Also, with "from background to foreground", do you mean navigating somewhere else in your android phone or staying in the Flutter app but opening another view and then going back to camera view?

Yes crash happens from both perspective from background to foreground as well as opening another view and then going back to camera view

Tested : ✅ one plus + 6 ✅ Pixel 4 emulator api 30

CariusLars commented 2 years ago

Hey guys, sorry for the late reply on this... It seems like someone found a solution to this problem: https://github.com/CariusLars/ar_flutter_plugin/issues/59 As soon as the PR is out I will review this and publish a new release version, hopefully that will fix your issues!