CsabaConsulting / ARPhysics

Testing Physics engines with AR
MIT License
2 stars 0 forks source link

NPE in dev.csaba.arphysics.MainActivity.onTransformChanged #9

Closed MrCsabaToth closed 3 years ago

MrCsabaToth commented 3 years ago

Reported by the Play Store. Interesting that the app was not installed from the Play Store but from some other channels, it came from a Samsung Galaxy A70 and ZTE Z717VL (Android 9 - API 28 and Android 10 API 29) devices. https://play.google.com/console/u/0/developers/7696534728259995393/app/4974857956571804228/vitals/crashes/d415f426/details?days=30

Call stack:

java.lang.NullPointerException: 
  at dev.csaba.arphysics.MainActivity.onTransformChanged (MainActivity.java:307)
  at com.google.ar.sceneform.Node.dispatchTransformChanged (Node.java:1431)
  at com.google.ar.sceneform.Node.markTransformChangedRecursively (Node.java:524)
  at com.google.ar.sceneform.Node.markTransformChangedRecursively (Node.java:533)
  at com.google.ar.sceneform.Node.setParent (Node.java:284)
  at dev.csaba.arphysics.MainActivity.lambda$clearScene$4 (MainActivity.java:437)
  at dev.csaba.arphysics.-$$Lambda$MainActivity$wFvLAfQoVpA5JteXk3ovHe6pqOg.accept (Unknown Source:2)
  at com.google.ar.sceneform.Node.callOnHierarchy (Node.java:1161)
  at com.google.ar.sceneform.NodeParent.callOnHierarchy (NodeParent.java:96)
  at dev.csaba.arphysics.MainActivity.clearScene (MainActivity.java:433)
  at dev.csaba.arphysics.MainActivity.lambda$initializeGallery$5$MainActivity (MainActivity.java:463)
  at dev.csaba.arphysics.-$$Lambda$MainActivity$OjuNocQDMm-AtmJE8pLxzIpG_IM.onClick (Unknown Source:2)
  at android.view.View.performClick (View.java:7870)
  at android.view.View.performClickInternal (View.java:7839)
  at android.view.View.access$3600 (View.java:886)
  at android.view.View$PerformClick.run (View.java:29363)
  at android.os.Handler.handleCallback (Handler.java:883)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:7814)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1068)
MrCsabaToth commented 3 years ago

Upon investigation the faulty code was the jBulletController.updateCylinderLocation inside the onTransformChanged. The onTransformChanged is the callback for Sceneform's TransformableNode (the way we handle this is that MainActivity extends AppCompatActivity implements Node.TransformChangedListener and we attach a listener manually cylinderNode.addTransformChangedListener(this);.

The safety check checks on cylinderNode != null but it seems that during Scene clear operation we must manually null the cylinderNode which we keep a pointer to ourselves. This tricked the safety checks. The Scene was mostly clear, but an unwanted cylinder node still kept alive (possibly due to our action of holding a strong pointer at it). The updates kept coming but we destroyed the Physics engine. I'm adding more safety checks and making sure taking care of the cylinderNode pointer. As an extra I call clearSceve from the onDestroy as well. I'm also making sure we detach the listener before I null out the node.