Additionally, it's not really shared ownership that causes problems; it's the fact that references (and pointers) make it possible to violate ownership semantics. References are used all the time in C++, and you can't use any libraries without them. It doesn't matter if data has a single owner if that owner is deleted and you still hold references to the data. In fact, shared ownership via shared_ptr is safer than unique_ptr, from a UAF point of view.
Shared ownership makes it much harder to reason about lifetimes, increasing the chance of mistakes.
> shared ownership via shared_ptr is safer than unique_ptr, from a UAF point of view.
Only if the dereferencing code holds the shared_ptr. It's also common to pass non-owning pointers to objects held by shared_ptr.
> When people use the delete operator manually and mess up, do they usually delete objects too early or too late?
As you pointed out, problems arise if objects are deleted too early or twice. Double free is common in C and can be exploited as well...
Well if it isn't getting any safer than the time-disproven technique of relying on human behaviour and there are automatically safer alternatives, then it is less safe than those alternatives.
Modern C++ is safer, but only if you can ensure everyone on the team never reaches for C style tricks, nor the libraries that you link against.
I think modern C++ is much safer than legacy C++, not by disallowing the USE after free, but by giving you the tools to make sure that the FREE happens at an appropriate time. A lot of this is not necessarily a feature of the language itself, but of idioms the community seems to agree to ie CppCoreGuidelines. I'm thinking of
- never use the delete operator manually, prefer std::unique_ptr
- prefer single ownership over shared ownership
- prefer immutable over mutable state
- ...
I agree with you that compared to Rust, this places a lot of burden on code reviewers and is in no way perfect, but I wouldn't say it's going in the wrong direction, making the language less safe.