@@ -157,13 +157,13 @@ impl<O: 'static> SignalVec for Box<dyn SignalVec<Item = O> + Send + Sync> {
157157/// Relevant in contexts where some function may require a [`Clone`] [`SignalVec`], but the concrete
158158/// type can't be known at compile-time, e.g. in a
159159/// [`.switch_signal_vec`](SignalExt::switch_signal_vec).
160- pub trait SignalVecClone : SignalVec + DynClone { }
160+ pub trait SignalVecDynClone : SignalVec + DynClone { }
161161
162- clone_trait_object ! ( < T > SignalVecClone < Item = T >) ;
162+ clone_trait_object ! ( <T > SignalVecDynClone < Item = T >) ;
163163
164- impl < T : SignalVec + Clone + ' static > SignalVecClone for T { }
164+ impl < T : SignalVec + Clone + ' static > SignalVecDynClone for T { }
165165
166- impl < O : ' static > SignalVec for Box < dyn SignalVecClone < Item = O > + Send + Sync > {
166+ impl < O : ' static > SignalVec for Box < dyn SignalVecDynClone < Item = O > + Send + Sync > {
167167 type Item = O ;
168168
169169 fn register_boxed_signal_vec ( self : Box < Self > , world : & mut World ) -> SignalHandle {
@@ -3480,7 +3480,7 @@ pub trait SignalVecExt: SignalVec {
34803480 /// } // without the `.boxed_clone()`, the compiler would not allow this
34813481 /// });
34823482 /// ```
3483- fn boxed_clone ( self ) -> Box < dyn SignalVecClone < Item = Self :: Item > + Send + Sync >
3483+ fn boxed_clone ( self ) -> Box < dyn SignalVecDynClone < Item = Self :: Item > + Send + Sync >
34843484 where
34853485 Self : Sized + Clone ,
34863486 {
@@ -3659,12 +3659,22 @@ impl<T> Clone for MutableVec<T> {
36593659
36603660impl < T > Drop for MutableVec < T > {
36613661 fn drop ( & mut self ) {
3662+ bevy_log:: info!( "cur: {}" , self . references. load( core:: sync:: atomic:: Ordering :: SeqCst ) ) ;
36623663 if self . references . fetch_sub ( 1 , core:: sync:: atomic:: Ordering :: SeqCst ) == 1 {
36633664 STALE_MUTABLE_VECS . lock ( ) . unwrap ( ) . push ( self . entity ) ;
36643665 }
36653666 }
36663667}
36673668
3669+ #[ derive( Component ) ]
3670+ struct MutableVecHandle < T > ( MutableVec < T > ) ;
3671+
3672+ impl < T > Clone for MutableVecHandle < T > {
3673+ fn clone ( & self ) -> Self {
3674+ Self ( self . 0 . clone ( ) )
3675+ }
3676+ }
3677+
36683678impl < T > MutableVec < T > {
36693679 /// Provides read-only access to the underlying [`Vec`] via either a `&World` or a
36703680 /// `&Query<MutableVecData<T>>`.
@@ -3721,7 +3731,17 @@ impl<T> MutableVec<T> {
37213731 } else {
37223732 vec![ ]
37233733 } ;
3724- world. entity_mut( * replay_signal) . insert( ( QueuedVecDiffs ( initial_diffs) , VecReplayTrigger ( trigger) ) ) ;
3734+
3735+ let mut replay_entity = world. entity_mut( * replay_signal) ;
3736+ replay_entity. insert( ( QueuedVecDiffs ( initial_diffs) , VecReplayTrigger ( trigger) ) ) ;
3737+
3738+ // --- FIX STARTS HERE ---
3739+ // By inserting a cloned handle onto the signal's entity, we tie the lifetime
3740+ // of the MutableVecData to the lifetime of the signal itself. When this
3741+ // signal is cleaned up and its entity is despawned, this component will be
3742+ // dropped, decrementing the reference count and allowing for proper cleanup.
3743+ replay_entity. insert( MutableVecHandle ( self_. clone( ) ) ) ;
3744+ // --- FIX ENDS HERE ---
37253745
37263746 pipe_signal( world, broadcaster_system, replay_signal) ;
37273747 replay_signal
@@ -3927,6 +3947,7 @@ mod tests {
39273947 use crate :: JonmoPlugin ;
39283948 use bevy:: prelude:: * ;
39293949 use bevy_platform:: sync:: RwLock ;
3950+ use test_log;
39303951
39313952 #[ derive( Resource , Default , Debug ) ]
39323953 struct SignalVecOutput < T : Clone + fmt:: Debug > ( Vec < VecDiff < T > > ) ;
@@ -4978,7 +4999,7 @@ mod tests {
49784999 handle. cleanup ( app. world_mut ( ) ) ;
49795000 }
49805001
4981- #[ test]
5002+ #[ test_log :: test]
49825003 fn test_filter_map ( ) {
49835004 // A comprehensive test for `SignalVecExt::filter_map`, covering all `VecDiff`
49845005 // types and the three status-change scenarios for `UpdateAt`.
@@ -5019,6 +5040,7 @@ mod tests {
50195040 // --- 3. Test `Push` ---
50205041 // Push a value that passes the filter_map ("30").
50215042 source_vec. write ( app. world_mut ( ) ) . push ( "30" ) ;
5043+ drop ( source_vec)
50225044 // app.update();
50235045 // let diffs = get_and_clear_output::<u32>(app.world_mut());
50245046 // assert_eq!(diffs.len(), 1);
0 commit comments