Skip to content

Commit 2e18588

Browse files
committed
Partially fix memory leak.
1 parent 263b2bc commit 2e18588

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/libstd/sys/unix/freertos/thread.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Thread {
3838
let join_mutex = Arc::new(Mutex::new());
3939
let state = Arc::new(AtomicU8::new(PENDING));
4040

41-
let arg = box (join_mutex.clone(), state.clone(), box p);
41+
let arg = box (join_mutex.clone(), state.clone(), p);
4242

4343
let name = name.unwrap_or_else(|| CStr::from_bytes_with_nul_unchecked(b"\0"));
4444

@@ -74,30 +74,29 @@ impl Thread {
7474

7575
extern "C" fn thread_start(arg: *mut libc::c_void) -> *mut libc::c_void {
7676
unsafe {
77-
let arg = Box::<(Arc<Mutex>, Arc<AtomicU8>, Box<Box<dyn FnOnce()>>)>::from_raw(
78-
arg as *mut _,
79-
);
80-
let (join_mutex, state, main) = *arg;
77+
let previous_state = {
78+
let (join_mutex, state, main) =
79+
*Box::from_raw(arg as *mut (Arc<Mutex>, Arc<AtomicU8>, Box<dyn FnOnce()>));
8180

82-
join_mutex.lock();
81+
join_mutex.lock();
8382

84-
state.store(RUNNING, SeqCst);
83+
state.store(RUNNING, SeqCst);
8584

86-
main();
87-
thread_local::cleanup();
85+
main();
86+
thread_local::cleanup();
8887

89-
let previous_state = state.swap(EXITED, SeqCst);
88+
let previous_state = state.swap(EXITED, SeqCst);
9089

91-
join_mutex.unlock();
90+
join_mutex.unlock();
9291

93-
// We drop these here manually since we don't know
94-
// if `vTaskDelete` will ensure they are dropped.
95-
drop(state);
96-
drop(join_mutex);
92+
previous_state
93+
};
9794

9895
if previous_state == DETACHED {
96+
drop(previous_state);
9997
vTaskDelete(ptr::null_mut());
10098
} else {
99+
drop(previous_state);
101100
vTaskSuspend(ptr::null_mut());
102101
}
103102
}

0 commit comments

Comments
 (0)