75 points · by jandeboevrie · 2y ago · 58 comments
C++26 is going to introduce erroneous behavior.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p27...
As for non-zero runtime overhead, that is long gone, since stuff like std::map, std::regexp and refusal from compiler vendors to break an ABI that isn't even defined on the ISO level, yet it blocks any proposal that has the side effect of breaking it.
I hope this renewed focus on safety will lead to the default for what is currently undefined behaviour being more in line with what was originally intended - sort-of arbitrary behaviour dependent on the platform's state at the time of code execution. As in, less constrained than "implementation defined", since there is no guarantee it will produce the same result every time, but more constrained than UB, since it does not invalidate the program.
The discipline required to not shoot your foot off in a non trivial project was far greater than than the extra code needed when writing plain C, or, for bigger projects, C# and Java.
I still reach for C first, and if I need higher level abstractions, I'm reaching for something other than C++.
Since there is no limitation on the no_destroy type, you can create any type with it, including those which have a non-trivial constructor. Of course, you could use a concept to restrain it to only trivially constructible types, or you could omit the launder in an if constexpr branch and still be compliant.
std::launder addresses a specific situation: when an object is replaced by another object of the same type but compiler doesn't know that the first object's lifetime has ended and the same address points to a different object. In this case compiler may still use cached values of constant or reference fields of the dead object or its vptr.
In this case the byte array merely provides storage for an object of type T created by the placement new operator and that object is never replaced by anything. The lifetime of the byte array itself doesn't end when a nested object is created.
The solution is to always terminate all threads before exiting. This requires a bit of internal infrastructure that's missing from many projects. If a cross-platform library is being used, it may start its own untracked threads.
On iOS, app processes only terminate in special circumstances. When this issue yields a crash it's (currently) imperceptible to the user. However, it's still tracked in the crash statistics.
- Executing static destructors without crashing is the ideal result.
- On Mac, there are factors that make the thread-termination approach preferable, such as using cross-platform libraries or a framework like Qt.
- On iOS, none of these apply, as process termination is out of your hands.
Worth noting that global constructors and destructors are generally sucky for a bunch of reasons. Don’t use them unless you really have to. Avoid them in libraries.
Reason to avoid global constructors: there’s no laziness to them; they definitely run on program start even if the global is never used. For small projects, no big deal. For large projects it means a large startup time penalty. Successful projects get big eventually, so if you’re playing the long game you want to assume you’re a big project. If you’re a library writer, it’s probable that you’ll be linked into something big. If you don’t have global ctors then your clients will thank you.
Atexit anything is just pointless, IMO. The best way for a process to exit is to just exit. Trying to “destruct” your state at exit risks race conditions and weird bugs but buys you very little. Even things like flushing or syncing a file probably don’t belong in atexit since you should assume your program will run a long time so you want important stuff flushed and synced way before the exit happens. Also, global dtors are implemented by having a global ctor that does __cxa_atexit, so see previous paragraph.
And as with all programming advice, your mileage may vary and some advice is best ignored. If global ctors/dtors achieve what you want and/or startup time isn’t an issue, then do what you feel, bro
I definitely want buffers to be flushed to disk if the user Ctrl-C's the program. The OS only ensures that the resources are released at the OS level, but does nothing to prevent the persistent application state becoming inconsistent at the application level.
The really hardcore way to do this is to structure your whole program so that all state is recorded as deltas appended to a WAL, with a background thread/process periodically merging this into "current state" -- in that case, even pulling the power cord can't leave your application in an inconsistent state. But that's a whole lot of work, and just best-effort flushing buffers on exit gets you 80% of the way there.
This is a pretty myopic view of things? There are lots of other things you might wish to do when the program terminates besides releasing resources...
Of course, I wouldn't put that clean up in an atexit.
Since it's unspecified, musl's behavior is just as correct as glibc's or BSD's or ...
I came to accept that in languages like C and C++, the best path for dynamic loading code is really via OS IPC and external processes, the decrease in hardware resources and simpler programming model of using shared libraries as plugins, isn't worth the endless amount of debugging hours regarding program crashes.
I actually have both a hard time imagining a better language (not saying it's impossible, just that it's not trivial for me to see) and also how ownership would resolve this issue. Could you elaborate?
libvirt has long been linked with -Wl,-z -Wl,nodelete to avoid this:
https://gitlab.com/libvirt/libvirt/-/commit/8e44e5593eb9b89f...
Another case in libnbd which really demonstrates how hard this is:
https://gitlab.com/nbdkit/libnbd/-/commit/368e3d0d5a8871aad5...
How is this different from holding a pointer to an object being deleted?
Haven’t looked into the bug closely but I will say that WebKit compiles with thread safety on statics turned off, which means they are in a non-standard language mode with far more races.
In many cases, they pull in 100+ kB of useless code that only runs on exit. I've only got 128 kiB of RAM and my program doesn't exit, but good luck convincing the compiler this is true. It's so aggravating.
Sadly, small oversights (e.g., exception handling for undefined virtual methods) can cause disproportionate bloat.
https://isocpp.org/wiki/faq/ctors#static-init-order
If you have two static objects in two different files then good luck if one depends on the other, as the initialization order is not well defined.
If both files are in the same executable then you can get around that by linking the object files in the desired order - well mostly, kind of.
i have hit this problem at some point...
Foo &getInstance() { static Foo instance; return instance; }
Guaranteed to be threadsafe by the standard, even.