4 ms·
I thought resources were freed when they went out of scope preventing any memory leaks in Rust.
by 0xdead 7y ago
I thought resources were freed when they went out of scope preventing any memory leaks in Rust.
- pornel 7y agoThey are, and in a typical program you don't need to worry about leaking memory. However, it is possible to leak, e.g. if you use a reference-counted type and create a cycle. There's also Box::leak() that does what it says (it's useful for singleton-like things).
- Rusky 7y agoThat's the common case, but you can still make a reference counting cycle, or have a thread hang while holding onto something, or even just call `mem::forget` on an allocation. This is different from Rust's guarantees about memory and thread safety, which you can't break without `unsafe`.
- kinghajj 7y agoYou can still introduce them via circular strong reference-counted pointers. The solution of course is to have only one strong reference and many weak ones, but the compiler in principle will let you create a cycle that will never get deallocated.