Skip to content

Commit

Permalink
prevent memory leak when dropping ParallelSystemContainer (bevyengine…
Browse files Browse the repository at this point in the history
…#2176)

`ParallelSystemContainer`'s `system` pointer was extracted from box, but it was never deallocated. This change adds missing drop implementation that cleans up that memory.
  • Loading branch information
Frizi authored and ostwilkens committed Jul 27, 2021
1 parent 686b98a commit 49c0c21
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/bevy_ecs/src/schedule/system_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
},
system::{ExclusiveSystem, System},
};
use std::{borrow::Cow, ptr::NonNull};
use std::{borrow::Cow, cell::UnsafeCell};

/// System metadata like its name, labels, order requirements and component access.
pub trait SystemContainer: GraphNode<Label = BoxedSystemLabel> {
Expand Down Expand Up @@ -106,7 +106,7 @@ impl SystemContainer for ExclusiveSystemContainer {
}

pub struct ParallelSystemContainer {
system: NonNull<dyn System<In = (), Out = ()>>,
system: Box<UnsafeCell<dyn System<In = (), Out = ()>>>,
pub(crate) run_criteria_index: Option<usize>,
pub(crate) run_criteria_label: Option<BoxedRunCriteriaLabel>,
pub(crate) should_run: bool,
Expand All @@ -123,7 +123,8 @@ unsafe impl Sync for ParallelSystemContainer {}
impl ParallelSystemContainer {
pub(crate) fn from_descriptor(descriptor: ParallelSystemDescriptor) -> Self {
ParallelSystemContainer {
system: unsafe { NonNull::new_unchecked(Box::into_raw(descriptor.system)) },
// SAFE: it is fine to wrap inner value with UnsafeCell, as it is repr(transparent)
system: unsafe { Box::from_raw(Box::into_raw(descriptor.system) as *mut _) },
should_run: false,
run_criteria_index: None,
run_criteria_label: None,
Expand All @@ -140,20 +141,19 @@ impl ParallelSystemContainer {
}

pub fn system(&self) -> &dyn System<In = (), Out = ()> {
// SAFE: statically enforced shared access.
unsafe { self.system.as_ref() }
// SAFE: statically enforced shared access
unsafe { self.system.get().as_ref().unwrap() }
}

pub fn system_mut(&mut self) -> &mut dyn System<In = (), Out = ()> {
// SAFE: statically enforced exclusive access.
unsafe { self.system.as_mut() }
self.system.get_mut()
}

/// # Safety
/// Ensure no other borrows exist along with this one.
#[allow(clippy::mut_from_ref)]
pub unsafe fn system_mut_unsafe(&self) -> &mut dyn System<In = (), Out = ()> {
&mut *self.system.as_ptr()
self.system.get().as_mut().unwrap()
}

pub fn should_run(&self) -> bool {
Expand Down

0 comments on commit 49c0c21

Please sign in to comment.