by AnthonyMouse · 1w ago · 0 comments
Suppose there is a point in the code where the compiler can't verify that it's safe to access the object but you want to do it anyway. You've transferred ownership of it to e.g. a third party library, while still keeping and using a naughty reference to it. Avoiding this would require you to restructure the code, which could be a lot of work. But the boss wants you to do it now instead of all the refactoring needed to do it right.
And everything's fine as long as the thing that does own the object doesn't release it before you expect it to. Just like C. Or maybe even worse, since now you're explicitly promising the library it can do what it likes with the object and then breaking your promise at your convenience.
> Suppose there is a point in the code where the compiler can't verify that it's safe to access the object but you want to do it anyway. You've transferred ownership of it to e.g. a third party library, while still keeping and using a naughty reference to it. Avoiding this would require you to restructure the code, which could be a lot of work. But the boss wants you to do it now instead of all the refactoring needed to do it right.
That's exactly what I thought every time I added 'unsafe' where I "knew" it was fine, even though the compiler told me I couldn't. And the vast majority of those times, within a day, I had to track down "the program now segfaults erratically". It's not _every_ time, but the vast majority of them. Certainly enough that when I now get into the same situation, the easier option is to make it certain that the object isn't being freed (wrap it in a refcount and call it a day, no unsafe in sight).
That's the point. Doing that isn't better but it causes the code to compile sooner, which is the thing that allows the boss to shovel it out the door faster in the shops where that's the priority.
And then when they do that all over the place it's still going to segfault.