Null parameter transfer on Android interface causes Unity to stop responding (ANR)

The accidental bug, after interfacing with Android students, always triggers the Android system ANR pop-up window: "xxx has no response", and then Unity Crash often occurs.

What is ANR

ANR is an error type in the Android system. When the interface thread of an application is blocked for too long, this error will be triggered and an ANR dialog box will appear: "xxx is unresponsive, do you want to close it?".

ANR will be triggered under the following two conditions:

  • When your activity is in the foreground, your application does not respond to input events or BroadcastReceiver (such as key press or screen tap events) within 5 seconds.
  • Although there is no activity in the foreground, your BroadcastReceiver has not been executed for a long time.

I was also surprised at this situation, because the Unity side did not write operations that consumed CPU time.

discover problems

In case of any problem, first grab the log, which is roughly as follows:

ANR Log:

 E CrashReport: ANR in com.xxxx(com.xxxx/com.xxxx.launcher) 11-25 14:58:57.939 22468 22530 E CrashReport: PID: 22468 11-25 14:58:57.939 22468 22530 E CrashReport: Reason: Input dispatching timed out (Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.)

Unity Crash(SIGSEGV):

 11-25 14:58:44.444 22468 22468 E CRASH   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00002458 11-25 14:58:44.444 22468 22468 E CRASH   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 11-25 14:58:44.444 22468 22468 E CRASH   : Build type 'Release', Scripting Backend 'mono', CPU 'armeabi-v7a' 11-25 14:58:44.444 22468 22468 E CRASH   : Build fingerprint: 'xxxxxxxxxxxxxxxx' 11-25 14:58:44.445 22468 22468 E CRASH   : Revision: '0' 11-25 14:58:44.445 22468 22468 E CRASH   : pid: 22468, tid: 22468, name: r.com.xxxx  >>> com.xxxx <<< 11-25 14:58:44.445 22468 22468 E CRASH   : r0 00000000  r1 00002458  r2 00000000  r3 80000000 11-25 14:58:44.445 22468 22468 E CRASH   : r4 00000000  r5 fff18810  r6 fff18810  r7 fff18d10 11-25 14:58:44.445 22468 22468 E CRASH   : r8 cddd0690  r9 cddd069c  sl fff18930  fp cddd0698 11-25 14:58:44.445 22468 22468 E CRASH   : ip ca6636a8  sp fff18808  lr c9dcd8f8  pc c9820e94  cpsr 000057c4 11-25 14:58:44.445 22468 22468 E CRASH   : 11-25 14:58:44.445 22468 22468 E CRASH   : backtrace: 11-25 14:58:44.452 22468 22468 E CRASH   :  #00  pc 0029ae94  /data/app/com/xxxx/lib/arm/libunity.so

After testing, it is found that the problem is caused by null parameter passed during Android interface callback. The following is an example of the problem:

Java code:

 //Declare an interface for Unity inheritance implementation package com.azimiao.test; public interface SomeListenerCallback{ void onSomethingHappen(String msg); }

Unity C # end code:

 //Inherit and implement Android interface in Unity public class SomeListenerCallback : AndroidJavaProxy { public SomeListenerCallback():base("com.azimiao.test.SomeListenerCallback") { } public void onSomethingHappen(string msg) { Debug. Log("DoSomething:" + (msg ?? "")); //other } }

At runtime, a SomeListenerCallback object is created and sent to the Android layer through the negotiated interface to save the reference for use.

At that time, we called the interface in a BroadcastReceiver callback:

 someListenerCallbackInstance.onSomethingHappen(msg);

In some cases, msg is null At this time, the program will show the ANR phenomenon, and the Unity Crash will appear when viewing the log.

Causes

Visible in the Unity Crash log SIGSEGV , on Diagnose native code crash problems As explained in the article, most of the signals are Pure null pointer dereference or Low address null pointer dereference Occurs when.

We use String to connect null, which is feasible in pure C # environment. However, when implementing the Android interface, the initial parameters connected to the engine are all AndroidJavaObject. When converting AndroidJavaObject to String in C #, the program null pointer call error causes the crash problem.

resolvent

After negotiation, check the parameters when calling the interface null Then change to "" (empty string).

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/8383.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. rantrism 12-10 11:29 reply

    Hello~I am the operator of Tencent Cloud+Community, and I have followed the technical articles you are sharing. I think the content is great. We sincerely invite you to join the Tencent Cloud We Media Sharing Program. For complete benefits and application address, please see: [Blocked link]
    After the author applies for this plan, the author will move his articles to the community column. You just need to fill in the form to apply. We will provide the author with traffic, ECS, domain names, etc., as well as some peripheral gifts. We sincerely invite you and look forward to your joining~