From 7b7b56398e12d4f77bfc6415ed284c9c5b9a2640 Mon Sep 17 00:00:00 2001 From: Tom Niget Date: Thu, 22 Dec 2022 14:03:18 +0100 Subject: [PATCH] Add footnote about memory leaks --- src/why-rust/compile-time.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/why-rust/compile-time.md b/src/why-rust/compile-time.md index b9aeed9f..d403aae6 100644 --- a/src/why-rust/compile-time.md +++ b/src/why-rust/compile-time.md @@ -3,10 +3,12 @@ Static memory management at compile time: * No uninitialized variables. -* No memory leaks. +* No memory leaks[^leaks]. * No double-frees. * No use-after-free. * No `NULL` pointers. * No forgotten locked mutexes. * No data races between threads. * No iterator invalidation. + +[^leaks]: It is technically possible to produce a memory leak in (safe) Rust. The [`Box::leak`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak) method allows getting a raw reference out of a [`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html) and dropping the [`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html) afterwards, without running the destructor. A use of this could be to get runtime-initialized and runtime-sized static variables. It is also possible to leak memory by creating a reference cycle, for example by using [`Rc`](https://doc.rust-lang.org/std/rc/struct.Rc.html) and a self-referential type (see [Chapter 15.6 in the Rust Book](https://doc.rust-lang.org/book/ch15-06-reference-cycles.html)). Or simply, the [`std::mem::forget`](https://doc.rust-lang.org/std/mem/fn.forget.html) function, which makes the compiler "forget" about a value meaning the destructor is never run. There are many other ways to create leaks in safe Rust, but for the purpose of this course, "No memory leaks" should be understood as "Pretty much no *accidental* memory leaks".