Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assets are dropped although strong handles are kept in a resource #2347

Closed
NiklasEi opened this issue Jun 15, 2021 · 3 comments
Closed

Assets are dropped although strong handles are kept in a resource #2347

NiklasEi opened this issue Jun 15, 2021 · 3 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior

Comments

@NiklasEi
Copy link
Member

Bevy version

Latest commit 71bf07f5c085b9c02cbe5337040337c3ed8b0002

Operating system & version

Ubuntu 18.04

What you did

Load png files in a State. When they are loaded, store strong handles to the textures in a resource and switch to a different state. Now I print out the asset loading state in each frame.

What you expected to happen

The asset handles should be kept in loaded state since there are strong handles for them stored in a resource.

What actually happened

The assets are unloaded after a very short time. The following output is one line per frame. From the third frame on the assets are no longer loaded.

AssetStates: player Loaded, tree Loaded
AssetStates: player Loaded, tree Loaded
AssetStates: player Unloaded, tree Unloaded
AssetStates: player Unloaded, tree Unloaded
AssetStates: player Unloaded, tree Unloaded
AssetStates: player Unloaded, tree Unloaded

Additional information

The code for a minimal example producing the above logs can be found here: https://github.com/NiklasEi/bevy_unloading_assets_example

I can consistently reproduce this issue in debug and in release mode.

@NiklasEi NiklasEi added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jun 15, 2021
@mockersf
Copy link
Member

it's expected, you're dropping the strong handles here: https://github.com/NiklasEi/bevy_unloading_assets_example/blob/main/src/main.rs#L14-L15

if you apply this diff you won't see the issue in your example:

diff --git a/src/main.rs b/src/main.rs
index dcc3720..494c71f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,8 +11,8 @@ fn check_asset_handles(asset_server: Res<AssetServer>, textures: Res<TextureAsse

 fn start_loading(mut commands: Commands, asset_server: Res<AssetServer>) {
     let mut handle_ids = vec![];
-    handle_ids.push(asset_server.load_untyped("textures/player.png").id);
-    handle_ids.push(asset_server.load_untyped("textures/tree.png").id);
+    handle_ids.push(asset_server.load_untyped("textures/player.png"));
+    handle_ids.push(asset_server.load_untyped("textures/tree.png"));

     commands.insert_resource(LoadingHandles {
         handles: handle_ids,
@@ -25,7 +25,9 @@ fn check_loading(
     mut state: ResMut<State<AppState>>,
     loading_handles: Res<LoadingHandles>,
 ) {
-    if asset_server.get_group_load_state(loading_handles.handles.clone()) == LoadState::Loaded {
+    if asset_server.get_group_load_state(loading_handles.handles.iter().map(|h| h.id))
+        == LoadState::Loaded
+    {
         commands.insert_resource(TextureAssets {
             player: asset_server.get_handle("textures/player.png"),
             tree: asset_server.get_handle("textures/tree.png"),
@@ -42,7 +44,7 @@ enum AppState {
 }

 struct LoadingHandles {
-    handles: Vec<HandleId>,
+    handles: Vec<HandleUntyped>,
 }

 struct TextureAssets {

@NathanSWard
Copy link
Contributor

@NiklasEi
Does @mockersf 's diff fix your problem?
If so, this (hopefully) doesn't seem to get a bug in bevy, in which case we can probably close this issue.

@NiklasEi
Copy link
Member Author

That explains a lot, thank you!
Not a bug in Bevy, yes. But definitely a bug in bevy_asset_loader 😅

@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds and removed S-Needs-Triage This issue needs to be labelled labels Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

4 participants