News for hackers. Stuff that matters.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by filmor on Sunday June 23, 2024 @05:50PM (#40770888)
The ? is not really the issue here. Rust is similar to C++ in that it encourages implicit resource management using scopes, so if you have resources that have to be free'd, you have to implement Drop somewhere.

The code would have looked just as correct and would have been just as wrong with the "old" non-sigil `try!(...)` syntax.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by akira2501 on Sunday June 23, 2024 @06:01PM (#40770971)
> The ? is not really the issue here.

Based upon the way the code was written, it was, at least in the mind of the author. They forgot they could exit scope there. They clearly didn't _intend_ for that outcome, but ended up with it anyways, possibly out of habit, and possibly because a single impactful sigil like that is easy to miss in review.

I get that technically it didn't cause the memory leak.. but just look at the way that was written initially... it obviously led to it _within_ that particular structure.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by nemetroid on Sunday June 23, 2024 @06:26PM (#40771137)
> They forgot they could exit scope there.

Their mistake was writing resource management code that requires manually thinking about where they might exit scope.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by oguz-ismail on Monday June 24, 2024 @12:51AM (#40772730)
Just curious, would writing it in a better language have prevented this?
Re:Fixing a memory leak of xmlEntityPtr in librsvg by Dx5IQ on Monday June 24, 2024 @01:44AM (#40772907)
This 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"

Re:Fixing a memory leak of xmlEntityPtr in librsvg by oguz-ismail on Monday June 24, 2024 @01:48AM (#40772917)
`finally' is a word though, harder to forget than a single punctuation mark.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by filmor on Monday June 24, 2024 @08:01AM (#40774988)
The 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.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by nemetroid on Monday June 24, 2024 @05:16AM (#40773918)
Slightly 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..., the first guideline in the "resource management" section.

2: https://doc.rust-lang.org/nomicon/intro.html