News for hackers. Stuff that matters.
Re:A Defer Mechanism for C by zabzonk on Monday December 14, 2020 @08:51AM (#25417463)
Destructors are not called randomly.
Re:A Defer Mechanism for C by greatgib on Monday December 14, 2020 @09:17AM (#25417760)
As replied by someone else, by 'random' I did not mean really 'randomly', because the machine is determinist and follow a logic.

But more that there is so much magic and abstractions that it is very hard for a dev to have a clear view of what is going on and what to expect. He has to 'guess' instead of just read the code. For that, I guess that it is similar to the current question of accountability of decisions made with deep learning algorithms.

As an example of my point, I would refer to the 'garbage collection' issues of a language like 'java'. GC will happen at a logically defined point like 'dirty mem > 100m' but from the developer point of view, his logic could suddenly lag unexpectedly in a middle of a simple operation because the GC was triggered by internal magic. It is very hard for a dev to be able to determine the memory usage at different points of the code and so have a certitude of when this operation could happen.

Re:A Defer Mechanism for C by zabzonk on Monday December 14, 2020 @09:57AM (#25418261)
> As an example of my point, I would refer to the 'garbage collection' issues of a language like 'java'

Two languages could hardly be less alike than Java and C++ - the latter is not garbage collected and where destructors are called is completely predictable.

Re:A Defer Mechanism for C by cle on Monday December 14, 2020 @10:19AM (#25418500)
You’re missing the point. The two are alike in that something significant is happening that you as the programmer did not explicitly tell it to do. The specific mechanism really isn’t an important distinction here.
Re:A Defer Mechanism for C by DougBTX on Monday December 14, 2020 @10:45AM (#25418819)
There is no guarantee when a Java finalizer will run, whereas a C++ destructor is guaranteed to run based on the code that you write. That's a big difference which has practical implications, using something with non-guaranteed behaviour as an example of why something with guaranteed behaviour is hard to predict seems like a misunderstanding.
Re:A Defer Mechanism for C by steveklabnik on Monday December 14, 2020 @01:06PM (#25420896)
I believe the situation is the same in C++, that is, if you have a shared_ptr cycle, the destructor will never be called, no?

It is more deterministic in C++, but you can still call exit(). (which is even more straightforward than the refcount cycle)

Re:A Defer Mechanism for C by jcelerier on Monday December 14, 2020 @02:01PM (#25421646)
> I believe the situation is the same in C++,

in the specific case of shared_ptr, which are a small part of codebases, if they are even used - for instance an immense amount of C++ GUI programs use Qt which doesn't use shared_ptr-like ownership semantics but instead a tree-of-objects model which does not have this issue. In contrast in Java / C# any object that has a reference to another is at risk.

Re:A Defer Mechanism for C by steveklabnik on Monday December 14, 2020 @06:32PM (#25424872)
Sure. That’s why I said “more deterministic.” But even a rare event disproves the guarantee.
Re:A Defer Mechanism for C by DougBTX on Tuesday December 15, 2020 @01:35PM (#25433349)
Sorry, I meant that the behaviour is guaranteed, not that the destructor is guaranteed to be called. So in the Rc cycle example the guarantee is that the destructor won’t be called.
Re:A Defer Mechanism for C by steveklabnik on Tuesday December 15, 2020 @06:28PM (#25436845)
Ah, then I misunderstood you, yes. My bad!
Re:A Defer Mechanism for C by cle on Monday December 14, 2020 @04:15PM (#25423414)
Of course it has practical implications and is harder to predict, but neither of those are what this conversation is about.
Re:A Defer Mechanism for C by zabzonk on Monday December 14, 2020 @10:36AM (#25418715)
If you write a destructor then you know exactly under what circumstances in C++ code it will be used (this is one of the major features of the language). This specific mechanism is extremely important.
Re:A Defer Mechanism for C by cle on Monday December 14, 2020 @10:46AM (#25418828)
Nobody is claiming that it isn’t well-defined nor unimportant.
Re:A Defer Mechanism for C by jcelerier on Monday December 14, 2020 @11:56AM (#25419787)
> something significant is happening that you as the programmer did not explicitly tell it to do.

when I put some object on automatic storage in C++ I do so because I explicitely want it to go away when its scope is left (either by reaching } or through an exception)

Re:A Defer Mechanism for C by sillysaurusx on Monday December 14, 2020 @09:02AM (#25417598)
"Smart pointers go brr"

It's not really random, but it's quite hard to follow.

Re:A Defer Mechanism for C by eropple on Monday December 14, 2020 @09:54AM (#25418197)
It shouldn't be. std::unique_ptr is very clear.

(std::shared_ptr should be used only when absolutely required and should be kept under close watch the whole time.)

Re:A Defer Mechanism for C by djmips on Monday December 14, 2020 @10:46AM (#25418820)
searches codebase I'm working on... finds thousands and thousands of std::shared_ptr (many involve a typedef, so there are even more...) eep!

Should I be alarmed?

Re:A Defer Mechanism for C by cesarb on Monday December 14, 2020 @10:58AM (#25419000)
If that codebase is older than C++0x, many of these std::shared_ptr could be taking the place of std::unique_ptr (which didn't exist back then; std::auto_ptr was a footgun). If you're in the mood for a refactoring, you could take a look at each one and see whether then can be replaced by std::unique_ptr, or whether they really have shared ownership semantics (which is what std::shared_ptr should be used for).
Re:A Defer Mechanism for C by usefulcat on Monday December 14, 2020 @11:29PM (#25427016)
Neither std::unique_ptr nor std::shared_ptr existed prior to c++11. Perhaps you're thinking of boost::shared_ptr or boost::scoped_ptr, both of which have existed in one form or another for somewhere around 20 years.
Re:A Defer Mechanism for C by Arnavion on Monday December 14, 2020 @02:55PM (#25422377)
Not necessarily. The concern about when the shared_ptr'd value runs its destructor only matters if the value's destructor is something you care about.

If you have a situation where a value needs to be shared between multiple other values, but also the value is trivial enough that it doesn't matter when it's destroyed or what it does when it's destroyed, but also the value is not so trivial that you can copy it instead of using a refcounting pointer, then std::shared_ptr is fine.

Re:A Defer Mechanism for C by eropple on Monday December 14, 2020 @02:59PM (#25422415)
"Fine" is probably pushing it, but "not the biggest problem you are likely to have" is probably true. But, like, it's easy to accidentally get yourself into situations where somebody forgot to use std::weak_ptr somewhere and you start leaking memory.

Unless you know you must share objects with multiple potential owners, though, std::unique_ptr is a lot wiser.

Re:A Defer Mechanism for C by Arnavion on Monday December 14, 2020 @03:01PM (#25422446)
Yes, when I wrote:

>but also the value is trivial enough that it doesn't matter when it's destroyed or what it does when it's destroyed"

that precludes the value from having other refcounting pointers that could then create cycles and leak.

Re:A Defer Mechanism for C by hedora on Monday December 14, 2020 @10:58AM (#25418997)
Yes. On reflection, or on ramping up new developers, I think you’ll find that it is unnecessarily difficult to reason about object lifetimes.

There’s a good chance that most of the shared_ptr’s can be replaced by unique_ptr, which has less runtime overhead. More importantly, it documents the intent of the programmer regarding ownership semantics, and the timespan during which the object should be remain valid.

Re:A Defer Mechanism for C by lultimouomo on Monday December 14, 2020 @09:35AM (#25417986)
That's really an argument against smart (to be precise, shared) pointers, not against destructors.
Re:A Defer Mechanism for C by ben509 on Monday December 14, 2020 @09:37AM (#25418011)
If it's too cold for you, it's too cold for your smart pointers. Bring your smart pointers inside during the winter months.