Skip to content

Return Application AssetManager from AndroidApp::asset_manager#233

Merged
rib merged 1 commit intomainfrom
rib/pr/sound-application-asset-manager
Mar 17, 2026
Merged

Return Application AssetManager from AndroidApp::asset_manager#233
rib merged 1 commit intomainfrom
rib/pr/sound-application-asset-manager

Conversation

@rib
Copy link
Member

@rib rib commented Mar 12, 2026

This makes sure that the AssetManager we return from AndroidApp::asset_manager can be retained with a static lifetime and never become a wrapper for an invalid pointer.

The key change here is that we now return the Application AssetManager (i.e. from Application.getAssets()) instead of the Activity AssetManager.

Theoretically there could be some applications that could associate an Activity AssetManager with unique resources but that's not expected to be common (and at least not expected to affect anyone currently using AndroidApp::asset_manager).

As part of the APP_ONCE initialization in init_android_main_thread we now get a global reference to the Application AssetManager and get the corresponding AAssetManager that we can trust will be valid for the lifetime of the process since we leak the global reference.

Note: The Application AssetManager is logically a process-wide resource and so the leaked global is just a technical formality to ensure it can't be garbage collected, but that's assumed to be redundant.

Note: If anyone strictly needs the Activity AssetManager then they could at least resort to calling Activity.getAssets() via JNI manually, but perhaps we can later consider adding a separate AndroidApp::activity_asset_manager() that will pair an AAssetManager pointer with a JNI global reference to ensure the pointer remains valid.

Fixes #161

@rib rib force-pushed the rib/pr/sound-application-asset-manager branch from 98c8bcf to abe08f7 Compare March 13, 2026 00:00
@rib rib force-pushed the rib/pr/run-on-java-main-thread branch 2 times, most recently from a087094 to 49bbf6c Compare March 17, 2026 10:23
Base automatically changed from rib/pr/run-on-java-main-thread to main March 17, 2026 10:27
@rib rib force-pushed the rib/pr/sound-application-asset-manager branch 2 times, most recently from 8806f7c to d562cc5 Compare March 17, 2026 11:10
This makes sure that the `AssetManager` we return from
`AndroidApp::asset_manager` can be retained with a static lifetime and
never become a wrapper for an invalid pointer.

The key change here is that we now return the Application AssetManager
(i.e. from Application.getAssets()) instead of the Activity
AssetManager.

Theoretically there could be some applications that could associate an
Activity AssetManager with unique resources but that's not expected to
be common (and at least no expected to affect anyone currently using
`AndroidApp::asset_manager`).

As part of the `APP_ONCE` initialization in `init_android_main_thread`
we now get a global reference to the Application AssetManager and get
the corresponding AAssetManager that we can trust will be valid for the
lifetime of the process since we leak the global reference.

Note: The Application `AssetManager` is logically a process-wide
resource and so the leaked global is just a technical formality to
ensure it can't be garbage collected, but that's assumed to be
redundant.

Note: If anyone _strictly_ needs the `Activity` `AssetManager` then they
could at least resort to calling `Activity.getAssets()` via JNI
manually, but perhaps we can later consider adding a separate
`AndroidApp::activity_asset_manager()` that will pair an `AAssetManager`
pointer with a JNI global reference to ensure the pointer remains valid.

Fixes #161
@rib rib force-pushed the rib/pr/sound-application-asset-manager branch from d562cc5 to 44bdb8b Compare March 17, 2026 11:19
@rib rib merged commit ae55532 into main Mar 17, 2026
7 checks passed
@rib rib deleted the rib/pr/sound-application-asset-manager branch March 17, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible unsoundness in AssetManager

1 participant