Track GameActivity android_app pointer lifetime more carefully#236
Merged
Conversation
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
b5ff3f5 to
d0779cc
Compare
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.
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_apppointer once we're notified that theGameActivityis 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:
Since this needed to add a hook to clear the android_app pointer after dispatching the callback for
MainEvent::Destroyit also made sense to fix the MainEvent::TerminateWindow hook for clearing ourNativeWindowso 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
Addresses: #196