|
|
|
|
|
by matvore
909 days ago
|
|
Don't worry, we enjoy that kind of stuff. I usually put all the resource freeing at the end of a function under a goto label, or only a few lines within the allocation, so it's easy to visually confirm everything is cleaned up. The way this commit frees the resources inside of if blocks is not how I would have done it. And if I find I let a leak in the code I usually refactor to make the correctness more obvious. In this example, the fact that m3_ParseModule takes ownership of the wasm pointer is very tricky. It looks like there is still a leak but there is not. |
|
Yes and no.
It's easy to not make a mistake in a function. It's basically impossible to not make this mistake in 1'000 functions.
Especially as code evolves over time, being 99% perfect about this, or even 99.9% perfect, is just not good enough. Not nearly enough.
And this reminds me of something Schneier said, that everyone can make a cryptographic algorithm that they themselves cannot break. That even as a professional, having your algorithm broken is not even embarrassing.
Similarly, very good coders are not even embarrassed when they fail at memory management. People may be better or worse at it, but even the best of us are terrible at it.
I once knew a very good coder who bragged about how apparently he's the only one able to write code that's not buggy like this.
To prove a point, I spent an hour reading his opensource project and found several resource leaks, at least one of which was remotely triggerable.
It's easy to do it right once, usually, but no these mistakes happen all the time.
> if I find [… typo words(?) omitted … ] a leak in the code I usually refactor to make the correctness more obvious.
This seems to contradict that it was easy to avoid in the first place. Yes, refactor can help. But there's a bit of learned helplessness, in that this is actually the language's fault, and it doesn't have to be that way.