Potential issue with standard main loop in NDK samples
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- android, cpp
- Domain
- mobile-dev
Research direction
Locate the Android NDK samples containing the android_main loop and inspect how ALooper_pollOnce, android_poll_source::process, and destroyRequested interact. Reproduce or reason through a destroy event, then update the affected loops so no frame is drawn after destruction and verify the samples still build and handle normal lifecycle events.
Written by the indexing model from the issue text.
Description
The main loop used in many Android NDK samples follows this standard structure:
void android_main(android_app* state)
{
...
while (!state->destroyRequested) {
android_poll_source* source = nullptr;
int result = ALooper_pollOnce(g_engine.IsReady() ? 0 : -1, nullptr, nullptr, (void**)&source);
if (result == ALOOPER_POLL_ERROR)
break;
if (source != nullptr)
source->process(state, source);
// Issue: This code is executed even after state->destroyRequested is set to true
if (g_engine.IsReady())
g_engine.DrawFrame();
}
...
}
Issue:
state->destroyRequested is set to true in source->process(state, source) when a "destroy" event occurs. However, the code continues executing after state->destroyRequested is set which can lead to undefined behavior, e.g. accessing released resources.
Potential Fix:
android_poll_source* source;
while (ALooper_pollOnce(g_engine.IsReady() ? 0 : -1, nullptr, nullptr, reinterpret_cast<void**>(&source)) != ALOOPER_POLL_ERROR) {
if (source != nullptr) {
source->process(state, source);
// Exit loop immediately after destroy is requested
if (state->destroyRequested != 0)
break;
}
if (g_engine.IsReady())
g_engine.DrawFrame();
}
- Dominant language
- C++
- Stars
- 10.5k
- Forks
- 4.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from android/ndk-samples
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
android/ndk-samples#1172 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
android/ndk-samples#1170 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
android/ndk-samples#1166 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
android/ndk-samples#1087 · 4 comments ·
-
resurrect PGO sampleMay be free again @DanAlbert claimed this 864 days ago, and no pull request is open. Open
android/ndk-samples#1070 · 1 assignee ·
All issues in android/ndk-samples
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
cp-algorithms/cp-algorithms#1774 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
telefonicaid/fiware-orion#4836 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
SanderMertens/flecs#2463 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 2 days
-
nixd exits at startup when its parent process belongs to another userPossibly taken @conao3 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nix-community/nixd#930 ·
Maintainers usually reply within 2 days