From 392795989a12d472d6531906017eace55da74417 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 11 Dec 2022 18:34:15 +0000 Subject: [PATCH] run clear trackers on render world (#6878) # Objective - Fixes https://github.com/bevyengine/bevy/issues/6417 ## Solution - clear_trackers was not being called on the render world. This causes the removed components vecs to continuously grow. This PR adds clear trackers to the end of RenderStage::Cleanup ## Migration Guide The call to `clear_trackers` in `App` has been moved from the schedule to App::update for the main world and calls to `clear_trackers` have been added for sub_apps in the same function. This was due to needing stronger guarantees. If clear_trackers isn't called on a world it can lead to memory leaks in `RemovedComponents`. --- crates/bevy_app/src/app.rs | 8 +++++--- crates/bevy_ecs/src/system/system_param.rs | 7 +++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index c6613825591ec..d88a82059126c 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -107,9 +107,7 @@ impl Default for App { #[cfg(feature = "bevy_reflect")] app.init_resource::(); - app.add_default_stages() - .add_event::() - .add_system_to_stage(CoreStage::Last, World::clear_trackers); + app.add_default_stages().add_event::(); #[cfg(feature = "bevy_ci_testing")] { @@ -150,9 +148,13 @@ impl App { #[cfg(feature = "trace")] let _bevy_frame_update_span = info_span!("frame").entered(); self.schedule.run(&mut self.world); + for sub_app in self.sub_apps.values_mut() { (sub_app.runner)(&mut self.world, &mut sub_app.app); + sub_app.app.world.clear_trackers(); } + + self.world.clear_trackers(); } /// Starts the application by calling the app's [runner function](Self::set_runner). diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 8ec8953d486f7..c9ef8ab840eb3 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -816,10 +816,9 @@ unsafe impl SystemParamState for LocalState { /// note that the `RemovedComponents` list will not be automatically cleared for you, /// and will need to be manually flushed using [`World::clear_trackers`] /// -/// For users of `bevy` itself, this is automatically done in a system added by `MinimalPlugins` -/// or `DefaultPlugins` at the end of each pass of the game loop during the `CoreStage::Last` -/// stage. As such `RemovedComponents` systems should be scheduled after the stage where -/// removal occurs but before `CoreStage::Last`. +/// For users of `bevy` and `bevy_app`, this is automatically done in `bevy_app::App::update`. +/// For the main world, [`World::clear_trackers`] is run after the main schedule is run and after +/// `SubApp`'s have run. /// /// # Examples ///