From e68b499ce357988db9017dcee474f975ee435f10 Mon Sep 17 00:00:00 2001 From: bofeng Date: Wed, 11 Dec 2024 15:53:41 +0800 Subject: [PATCH 1/4] Fix potential ANR issues caused by unsafe calls to the ALooper_pollAll interface. --- .../platform/android/AndroidPlatform.cpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index d1bd348a45d..b97b5c38c46 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -849,15 +849,24 @@ void AndroidPlatform::exit() { int32_t AndroidPlatform::loop() { IXRInterface *xr = CC_GET_XR_INTERFACE(); while (true) { - int events; struct android_poll_source *source; // suspend thread while _loopTimeOut set to -1 - while ((ALooper_pollAll(_loopTimeOut, nullptr, &events, - reinterpret_cast(&source))) >= 0) { - // process event - if (source != nullptr) { - source->process(_app, source); + while (true) { + int pollResult = ALooper_pollOnce(_loopTimeOut, nullptr, nullptr, + reinterpret_cast(&source)); + + // Process events if any + if (pollResult == ALOOPER_POLL_ERROR) { + CC_LOG_ERROR("ALooper_pollOnce returned and error"); + break; + } else { + if (source != nullptr) { + source->process(_app, source); + } + if (pollResult == ALOOPER_POLL_TIMEOUT) { + break; + } } // Exit the game loop when the Activity is destroyed From cba08b71aeb1be0bd488ad676f7f3ba5ee48d7b8 Mon Sep 17 00:00:00 2001 From: bofeng Date: Wed, 11 Dec 2024 16:14:15 +0800 Subject: [PATCH 2/4] refine --- native/cocos/platform/android/AndroidPlatform.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index b97b5c38c46..76836cf7634 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -860,14 +860,14 @@ int32_t AndroidPlatform::loop() { if (pollResult == ALOOPER_POLL_ERROR) { CC_LOG_ERROR("ALooper_pollOnce returned and error"); break; - } else { - if (source != nullptr) { - source->process(_app, source); - } - if (pollResult == ALOOPER_POLL_TIMEOUT) { - break; - } } + if (source != nullptr) { + source->process(_app, source); + } + if (pollResult == ALOOPER_POLL_TIMEOUT) { + break; + } + // Exit the game loop when the Activity is destroyed if (_app->destroyRequested) { From 11a2357b8f62692692b51e921f242eedcbcfc0e4 Mon Sep 17 00:00:00 2001 From: bofeng Date: Thu, 12 Dec 2024 16:31:33 +0800 Subject: [PATCH 3/4] refine --- .../cocos/platform/android/AndroidPlatform.cpp | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 76836cf7634..7a84ace962d 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -852,22 +852,12 @@ int32_t AndroidPlatform::loop() { struct android_poll_source *source; // suspend thread while _loopTimeOut set to -1 - while (true) { - int pollResult = ALooper_pollOnce(_loopTimeOut, nullptr, nullptr, - reinterpret_cast(&source)); - - // Process events if any - if (pollResult == ALOOPER_POLL_ERROR) { - CC_LOG_ERROR("ALooper_pollOnce returned and error"); - break; - } + while (ALooper_pollOnce(_loopTimeOut, nullptr, nullptr, + reinterpret_cast(&source)) >= 0) { + // Process events if (source != nullptr) { source->process(_app, source); } - if (pollResult == ALOOPER_POLL_TIMEOUT) { - break; - } - // Exit the game loop when the Activity is destroyed if (_app->destroyRequested) { From 32731f487d01c0ab5dab27da38728ab27abde4bf Mon Sep 17 00:00:00 2001 From: bofeng Date: Thu, 12 Dec 2024 16:32:58 +0800 Subject: [PATCH 4/4] refine --- native/cocos/platform/android/AndroidPlatform.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 7a84ace962d..335c90d189d 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -854,7 +854,7 @@ int32_t AndroidPlatform::loop() { // suspend thread while _loopTimeOut set to -1 while (ALooper_pollOnce(_loopTimeOut, nullptr, nullptr, reinterpret_cast(&source)) >= 0) { - // Process events + // Process event if (source != nullptr) { source->process(_app, source); }