From cf505e99cdbd22fccf2fbf09e47e8266c276017b Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 7 Jan 2023 16:45:24 +0100 Subject: [PATCH] =?UTF-8?q?Rephrase=20=E2=80=9CNo=20memory=20leaks?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The footnote was added before we had support for speaker notes. It’s too large to show on the screen during a classroom presentation, so it has now been moved to the speaker notes. I tried to keep the information intact, including keeping the word “mostly” on the slide. --- src/why-rust/compile-time.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/why-rust/compile-time.md b/src/why-rust/compile-time.md index e80a62ff..f195cbdd 100644 --- a/src/why-rust/compile-time.md +++ b/src/why-rust/compile-time.md @@ -3,7 +3,7 @@ Static memory management at compile time: * No uninitialized variables. -* *Mostly* no memory leaks[^leaks]. +* No memory leaks (_mostly_, see notes). * No double-frees. * No use-after-free. * No `NULL` pointers. @@ -11,4 +11,23 @@ Static memory management at compile time: * 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. 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". +
+ +It is possible to produce memory leaks in (safe) Rust. Some examples +are: + +* You can for use [`Box::leak`] to leak a pointer. A use of this could + be to get runtime-initialized and runtime-sized static variables +* You can use [`std::mem::forget`] to make the compiler "forget" about + a value (meaning the destructor is never run). +* You can also accidentally create a [reference cycle] with `Rc` or + `Arc`. + +For the purpose of this course, "No memory leaks" should be understood +as "Pretty much no *accidental* memory leaks". + +[`Box::leak`]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak +[`std::mem::forget`]: https://doc.rust-lang.org/std/mem/fn.forget.html +[reference cycle]: https://doc.rust-lang.org/book/ch15-06-reference-cycles.html + +