3 ms·
> They forgot they could exit scope there. Their mistake was writing resource management code that requires manually thinking about where they might exit scope
by nemetroid 2y ago
> They forgot they could exit scope there.
Their mistake was writing resource management code that requires manually thinking about where they might exit scope.
- oguz-ismail 2y agoJust curious, would writing it in a better language have prevented this?
- Dx5IQ 2y agoThis is a classic problem you can make in any language. If you were doing Java, and forgot to use `finally` for a resource, you'd have a leak due to an exception. I do not know what metric you use to define "better"
- oguz-ismail 2y ago`finally' is a word though, harder to forget than a single punctuation mark.
- filmor 2y agoThe dev did not "forget a single punctuation mark". It was there and it was correct. What was /not/ correct is that he did not implement any form of implicit resource freeing in an RAII-language. Doing resource management using scopes is a choice. It's not inherently better or worse than explicit resource management with something like Go's `defer`. But if that choice is made, as in Rust or C++, you have to ensure that the implicit resource management is correctly implemented (Drop in Rust, destructors in C++). Same as in Go, where you have to ensure that you call `defer` with the correct cleanup function or in C# where you have to use `using` on your disposables, etc.
- nemetroid 2y agoSlightly tongue in cheek answer: it is easy to write this kind of code in C++ without having C involved, hence style guides will usually have a prominent guideline specifically against this type of code[1]. In Rust, I think you only really run into this issue when interacting with C (or otherwise engaging in unsafe code), so for normal Rust coding it doesn't need to be spelled out as a guideline. And the Rustonomicon[2], the go-to resource for unsafe Rust, isn't really written as a set of guidelines. At least from a brief search, I found it harder to find a Rust page that specifically says "don't do this". 1: E.g. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-raii https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines..., the first guideline in the "resource management" section. 2: https://doc.rust-lang.org/nomicon/intro.html https://doc.rust-lang.org/nomicon/intro.html