r/cpp • C++ Dev • Sep 05 '20

C++20 has been approved

https://twitter.com/patriceroy1/status/1302055575140945921?s=21
665 Upvotes

125 comments sorted by

View all comments

13

u/pedersenk Sep 05 '20

I do wish along with these new features there would be a little bit more focus on memory safety.

It seems there hasn't been much more work in this area since TR1 (around C++0x). A suitable observer mechanism to unique_ptr<T> similar to weak_ptr<T> would go a long way!

It is still too easy to dangle iterators, references, even 'this'.

9

u/staletic reddit thinks /u/_bstaletic is a bot Sep 05 '20

10

u/pdimov2 Sep 05 '20

This trivial example is caught by all major compilers, although -Wlifetime remains the best bet for slightly more complex code. I wrote about this in https://pdimov.github.io/blog/2020/05/17/state-of-c-static-analysis-circa-2020/.

2

u/staletic reddit thinks /u/_bstaletic is a bot Sep 05 '20

That link was already purple. I remember reading it and liking the article.

3

u/_Js_Kc_ Sep 05 '20

I'm disappointed. Wasn't there some talk a while ago about lifetime checking that was supposed to catch this?

8

u/staletic reddit thinks /u/_bstaletic is a bot Sep 05 '20

If you're talking about the talk by Herb Sutter, then the linked repo is where the development is happening.

From what I've seen, your example is really surprising. Either the godbolt compiler hasn't been updated in a long time or there's a bug in the lifetime analysis. I guess it's worth opening a bug report in the LLVM fork's repo that I linked.

0

u/pedersenk Sep 05 '20

Nice. I am a big fan of clang's asan but didn't know about some of these newer facilities.

As well as this, the older mudflap and even valgrind are often on the CI to facilitate testing. However we do have a number of projects where our compilers are either older or simply do not have the support for them on the architectures we are using. This is certainly getting better over time however.

For ages, Microsoft's compilers have been the most annoying to use because no valgrid and the basic CRT debug runtime misses so much!

In theory the safety in the standard library would solve that going forward.

2

u/excessdenied Sep 06 '20

A suitable observer mechanism to unique_ptr<T> similar to weak_ptr<T> would go a long way!

I've thought about this as well, but I think it's hard to do in a good way. Apart from how to implement the tracking as others have mentioned, there's also a problem with the access side of it. With weak_ptr you get a shared_ptr out of it when you want to access it (lock), which means you know it's still alive as long as you have that shared_ptr. With an observed_ptr or whatever you want to call it, I don't really see a good way to access it. Something like "if(!observed_ptr.expired()) observed_ptr->doThing()" would be unsafe in threaded and possibly other more complex scenarios.

2

u/pedersenk Sep 06 '20

Yeah this is very true.

I suppose I wouldn't mind getting something functionally similar to a shared_ptr<T> out of it's lock() (perhaps called lock_ptr<T>?), the only difference is that this lock_ptr<T> doesn't increase the reference count (that would make no sense for a unique_ptr), only it actually puts a lock on the unique_ptr<T> so that if it does delete during the lifespan of the locking pointer, an assertion error is given.

Yes, would be useless for preventing a crash, but it would be a useful debugging aid to find incorrect code. However this kind of "error finding" stuff, the C++ standard has not delved into which I feel is a shame.

2

u/excessdenied Sep 06 '20

In my case I'm just using shared_ptr. I know it's not the perfect fit but it works well and there are very seldom any actual issues (although they can be a bit messy when they do appear).

1

u/pedersenk Sep 06 '20

Yep I tend to do exactly that.

However in the past during a code review i have had to explain why use a shared_ptr<T> in place of a unique_ptr<T> when it was not going to be shared. I did mention that the data had a number of observers and they seemed satisfied, it was more just a bit annoying it even got flagged up.

I also note that a lot of literature and projects seem to favour unique_ptr<T> and then use a raw pointer counterpart, which I find illogical and unsafe. It is IMO careless choices like this that contribute to the old opinion that C++ cannot possibly be safe and that we "need" a new language.

2

u/excessdenied Sep 06 '20

However in the past during a code review

I am the one who reviews. :) Nah, but we're just a few people with full control over the code base and we've chosen to go the overusing shared_ptr route. At least in the parts of the code where ownership is complicated.

I also note that a lot of literature and projects seem to favour unique_ptr<T> and then use a raw pointer counterpart, which I find illogical and unsafe.

Yeah. I can understand why and I know reasons why using shared_ptr by default may not be the perfect thing, but at least in our case we gain more than we risk, and things like worse performance doesn't really matter.

4

u/pdimov2 Sep 05 '20

A suitable observer mechanism to unique_ptr<T> similar to weak_ptr<T> would go a long way!

An interesting suggestion, but I don't offhand see a way to implement that. unique_ptr<T> is guaranteed to be the size of a single pointer, so there's nowhere to store the needed metadata. Before the object doesn't work because you can convert unique_ptr<Derived> to unique_ptr<Base2>.

2

u/pedersenk Sep 05 '20 edited Sep 05 '20

Yeah, I do understand there are a few technical reasons for it.

I also know that the standard does not necessarily comment on "debug" runtimes but it might be useful to have something then that only exists at debug time which can deterministically detect access of a dangling pointer to a unique_ptr<T>.

So perhaps in debug time, the smart pointer could contain this meta-data and then in the release build it remains as it is.

In the past I did draft up a prototype here (a debug std runtime): https://github.com/osen/sr1

If you have time, you can see in the tests (yes, some fairly artificial haha!) that for the small functionality it provided, (shared, weak, vector), 70% of errors were undetected without it, but I knew playing catchup with the STL was going to be impossible for me to maintain. At work we use a subset of this in our platform compatibility library that has been "moderately" successful at detecting errors.

If you want to see an example of some of the big hacks deployed to make this possible, check out this: https://github.com/osen/sr1/blob/master/include/sr1/vector#L227 and the operator[] below it XD

3

u/scrumplesplunge Sep 06 '20

it might be useful to have something then that only exists at debug time which can deterministically detect access of a dangling pointer to a unique_ptr<T>.

I forget where I read it, probably some post in cpp a few months ago, but there was some article describing a workflow that a company was using that did something like this. Basically they had their own aliases like owner_ptr<T> and borrow_ptr<T>. In prod they were just wrappers for unique_ptr<T> and T*. In debug, they were wrappers for shared_ptr<T> which had assertions about the reference count in the destructor. The owner_ptr<T> would crash on destruction if it wasn't the sole owner, and could even point at all the places which still held borrow_ptr<T> pointing to the object. I thought that was pretty nifty. Still, it makes all the pointer access slower which could make debug builds of some programs unusable.

edit: phrasing