Skip to content

Commit

Permalink
In Thread::new, add a comment that a panic could cause a memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vytautas Astrauskas committed Apr 1, 2020
1 parent 5382347 commit baa6d55
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/libstd/sys/cloudabi/thread.rs
Expand Up @@ -31,12 +31,15 @@ impl Thread {
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);

let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);

return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
let _ = Box::from_raw(p);
drop(Box::from_raw(p));
Err(io::Error::from_raw_os_error(ret))
} else {
Ok(Thread { id: native })
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/hermit/thread.rs
Expand Up @@ -61,7 +61,7 @@ impl Thread {
return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
let _ = Box::from_raw(p);
drop(Box::from_raw(p));
Err(io::Error::new(io::ErrorKind::Other, "Unable to create thread!"))
} else {
Ok(Thread { tid: tid })
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/sys/unix/thread.rs
Expand Up @@ -64,12 +64,15 @@ impl Thread {
};

let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);

return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
let _ = Box::from_raw(p);
drop(Box::from_raw(p));
Err(io::Error::from_raw_os_error(ret))
} else {
Ok(Thread { id: native })
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/sys/vxworks/thread.rs
Expand Up @@ -52,12 +52,15 @@ impl Thread {
};

let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);

return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
let _ = Box::from_raw(p);
drop(Box::from_raw(p));
Err(io::Error::from_raw_os_error(ret))
} else {
Ok(Thread { id: native })
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/thread.rs
Expand Up @@ -41,7 +41,7 @@ impl Thread {
return if ret as usize == 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
let _ = Box::from_raw(p);
drop(Box::from_raw(p));
Err(io::Error::last_os_error())
} else {
Ok(Thread { handle: Handle::new(ret) })
Expand Down

0 comments on commit baa6d55

Please sign in to comment.