Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jan 23, 2023
1 parent 716ffab commit 40688e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 10 additions & 0 deletions boa_gc/src/internals/ephemeron_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ pub(crate) struct EphemeronBox<K: Trace + ?Sized + 'static, V: Trace + 'static>
data: Cell<Option<NonNull<Data<K, V>>>>,
}

impl<K: Trace + ?Sized + 'static, V: Trace + 'static> Drop for EphemeronBox<K, V> {
fn drop(&mut self) {
if let Some(data) = self.data.take() {
// SAFETY: `data` comes from an `into_raw` call, so this pointer is safe to pass to
// `from_raw`.
drop(unsafe { Box::from_raw(data.as_ptr()) });
}
}
}

struct Data<K: Trace + ?Sized + 'static, V: Trace + 'static> {
key: NonNull<GcBox<K>>,
value: V,
Expand Down
15 changes: 12 additions & 3 deletions boa_gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,22 @@ impl Collector {
// Not initializing a dropguard since this should only be invoked when BOA_GC is being dropped.
let _guard = DropGuard::new();

let sweep_head = &gc.strong_start;
while let Some(node) = sweep_head.get() {
let strong_head = &gc.strong_start;
while let Some(node) = strong_head.get() {
// SAFETY:
// The `Allocator` must always ensure its start node is a valid, non-null pointer that
// was allocated by `Box::from_raw(Box::new(..))`.
let unmarked_node = unsafe { Box::from_raw(node.as_ptr()) };
sweep_head.set(unmarked_node.header.next.take());
strong_head.set(unmarked_node.header.next.take());
}

let eph_head = &gc.weak_start;
while let Some(node) = eph_head.get() {
// SAFETY:
// The `Allocator` must always ensure its start node is a valid, non-null pointer that
// was allocated by `Box::from_raw(Box::new(..))`.
let unmarked_node = unsafe { Box::from_raw(node.as_ptr()) };
eph_head.set(unmarked_node.header().next.take());
}
}
}
Expand Down

0 comments on commit 40688e6

Please sign in to comment.