Skip to content

Commit 3850705

Browse files
committed
Fix memory leak in detached threads.
1 parent 2e18588 commit 3850705

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

+6-7
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(), p);
41+
let arg = box (Arc::clone(&join_mutex), Arc::clone(&state), p);
4242

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

@@ -56,9 +56,7 @@ impl Thread {
5656
);
5757

5858
if res != pdTRUE {
59-
if thread.state.load(SeqCst) == PENDING {
60-
drop(Box::from_raw(arg));
61-
}
59+
drop(Box::from_raw(arg));
6260

6361
if res == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY {
6462
return Err(io::Error::new(
@@ -79,8 +77,7 @@ impl Thread {
7977
*Box::from_raw(arg as *mut (Arc<Mutex>, Arc<AtomicU8>, Box<dyn FnOnce()>));
8078

8179
join_mutex.lock();
82-
83-
state.store(RUNNING, SeqCst);
80+
state.compare_and_swap(PENDING, RUNNING, SeqCst);
8481

8582
main();
8683
thread_local::cleanup();
@@ -160,7 +157,9 @@ impl Thread {
160157
unsafe {
161158
assert!(self.id != xTaskGetCurrentTaskHandle());
162159

163-
while self.state.load(SeqCst) == PENDING {}
160+
while self.state.load(SeqCst) == PENDING {
161+
Self::yield_now()
162+
}
164163

165164
// Just wait for the thread to finish, the rest is handled by `Drop`.
166165
self.join_mutex.lock();

0 commit comments

Comments
 (0)