forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
88: Zero vector contents after popping r=ltratt a=jacob-hughes Since Alloy is a conservative GC, this prevents memory leaks kept alive by stale vector elements. Co-authored-by: Jake Hughes <jh@jakehughes.uk>
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// run-pass | ||
// ignore-tidy-linelength | ||
#![feature(gc)] | ||
#![feature(rustc_private)] | ||
#![feature(negative_impls)] | ||
#![feature(allocator_api)] | ||
#![allow(unused_assignments)] | ||
#![allow(unused_variables)] | ||
|
||
use std::gc::{Gc, GcAllocator}; | ||
use std::sync::atomic::{self, AtomicUsize}; | ||
|
||
struct Finalizable(usize); | ||
|
||
impl Drop for Finalizable { | ||
fn drop(&mut self) { | ||
FINALIZER_COUNT.fetch_add(1, atomic::Ordering::Relaxed); | ||
} | ||
} | ||
|
||
static FINALIZER_COUNT: AtomicUsize = AtomicUsize::new(0); | ||
|
||
fn test_pop(v: &mut Vec<Gc<Finalizable>, GcAllocator>) { | ||
for i in 0..10 { | ||
let mut gc = Some(Gc::new(Finalizable(i))); | ||
v.push(gc.unwrap()); | ||
gc = None; | ||
} | ||
|
||
for _ in 0..10 { | ||
let mut _gc = Some(v.pop()); | ||
_gc = None; | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut v1 = Vec::with_capacity_in(10, GcAllocator); | ||
test_pop(&mut v1); | ||
test_pop(&mut v1); | ||
|
||
GcAllocator::force_gc(); | ||
|
||
assert_eq!(FINALIZER_COUNT.load(atomic::Ordering::Relaxed), 20); | ||
} |