Track ANativeActivity pointer lifetime more carefully#234
Merged
Conversation
Once `on_destroy()` returns then the `NativeActivity.java` code will call an `unloadNativeCode` native method that will `delete` the `ANativeActivity` and invalidate any pointers we hold. Considering the possibility that an `AndroidApp` could be retained beyond the lifetime of the original `NativeActivity`, this ensures we always hold the `WaitableNativeActivityState::mutex` before dereferencing this pointer and ensures we clear the pointer before returning from `on_destroy` so we're also able to perform `null` pointer checks before dereferencing. Considering that `AndroidApp::vm_as_ptr` previously depended on dereferencing the `ANativeActivity`, this updates it to instead use `JavaVM::singleton()` which we guarantee will be initialized before the `AndroidApp` is created. Considering that `AndroidApp::activity_as_ptr()` promises to return a global reference that remains valid for the lifetime of the `AndroidApp`, but the `ANativeActivity::clazz` reference is deleted after `on_destroy()` returns, we now create our own `Global` reference for the `Activity` that is owned by `AndroidAppInner`.
f19460c to
c60af42
Compare
rib
added a commit
that referenced
this pull request
Mar 18, 2026
Most of the same issues found in the native-activity backend when working on #234 (to safely drop ANativeActivity via onDestroy callback) also apply to the game-activity backend, which this PR addresses. This ensures that the game-activity backend cleanly drops its `android_app` pointer once we're notified that the `GameActivity` is being destroyed and adds a mutex around the pointer that guarantees that it can't be freed while it's being dereferenced (because the same lock is required to respond to the onDestroy callback where the state gets freed). This makes a number of backend details consistent with the native-activity backend: - The backend retains its own Looper reference instead of relying on the android_app reference. - The backend allocates its own JNI global reference for the Activity, instead of relying on the android_app reference. Since this needed to add a hook to clear the android_app pointer after dispatching the callback for `MainEvent::Destroy` it also made sense to fix the MainEvent::TerminateWindow hook for clearing our `NativeWindow` so it also happens _after_ the callback (as the API docs state). Testing these changes with a minimal agdk-mainloop and agdk-egui example I see it's now possible to cleanly handle repeated activity start -> destroy -> start -> destroy cycles (e.g. due to config changes triggering a recreation of the activity). (When testing egui I did also have to patch Winit to ensure it exits the loop when receiving a Destroy event) Fixes: #235 Fixes: #162
rib
added a commit
that referenced
this pull request
Mar 18, 2026
Most of the same issues found in the native-activity backend when working on #234 (to safely drop ANativeActivity via onDestroy callback) also apply to the game-activity backend, which this PR addresses. This ensures that the game-activity backend cleanly drops its `android_app` pointer once we're notified that the `GameActivity` is being destroyed and adds a mutex around the pointer that guarantees that it can't be freed while it's being dereferenced (because the same lock is required to respond to the onDestroy callback where the state gets freed). This makes a number of backend details consistent with the native-activity backend: - The backend retains its own Looper reference instead of relying on the android_app reference. - The backend allocates its own JNI global reference for the Activity, instead of relying on the android_app reference. Since this needed to add a hook to clear the android_app pointer after dispatching the callback for `MainEvent::Destroy` it also made sense to fix the MainEvent::TerminateWindow hook for clearing our `NativeWindow` so it also happens _after_ the callback (as the API docs state). Testing these changes with a minimal agdk-mainloop and agdk-egui example I see it's now possible to cleanly handle repeated activity start -> destroy -> start -> destroy cycles (e.g. due to config changes triggering a recreation of the activity). (When testing egui I did also have to patch Winit to ensure it exits the loop when receiving a Destroy event) Fixes: #235 Fixes: #162
rib
added a commit
that referenced
this pull request
Mar 18, 2026
Most of the same issues found in the native-activity backend when working on #234 (to safely drop ANativeActivity via onDestroy callback) also apply to the game-activity backend, which this PR addresses. This ensures that the game-activity backend cleanly drops its `android_app` pointer once we're notified that the `GameActivity` is being destroyed and adds a mutex around the pointer that guarantees that it can't be freed while it's being dereferenced (because the same lock is required to respond to the onDestroy callback where the state gets freed). This makes a number of backend details consistent with the native-activity backend: - The backend retains its own Looper reference instead of relying on the android_app reference. - The backend allocates its own JNI global reference for the Activity, instead of relying on the android_app reference. Since this needed to add a hook to clear the android_app pointer after dispatching the callback for `MainEvent::Destroy` it also made sense to fix the MainEvent::TerminateWindow hook for clearing our `NativeWindow` so it also happens _after_ the callback (as the API docs state). Testing these changes with a minimal agdk-mainloop and agdk-egui example I see it's now possible to cleanly handle repeated activity start -> destroy -> start -> destroy cycles (e.g. due to config changes triggering a recreation of the activity). (When testing egui I did also have to patch Winit to ensure it exits the loop when receiving a Destroy event) Fixes: #235 Fixes: #162
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Once
on_destroy()returns then theNativeActivity.javacode will call anunloadNativeCodenative method that willdeletetheANativeActivityand invalidate any pointers we hold.Considering the possibility that an
AndroidAppcould be retained beyond the lifetime of the originalNativeActivity, this ensures we always hold theWaitableNativeActivityState::mutexbefore dereferencing this pointer and ensures we clear the pointer before returning fromon_destroyso we're also able to performnullpointer checks before dereferencing.Considering that
AndroidApp::vm_as_ptrpreviously depended on dereferencing theANativeActivity, this updates it to instead useJavaVM::singleton()which we guarantee will be initialized before theAndroidAppis created.Considering that
AndroidApp::activity_as_ptr()promises to return a global reference that remains valid for the lifetime of theAndroidApp, but theANativeActivity::clazzreference is deleted afteron_destroy()returns, we now create our ownGlobalreference for theActivitythat is owned byAndroidAppInner.Addresses: #196