Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 3
    The beauty of (modern) garbage collectors is that you don't really need to think about cyclic references. Such groups of Objects that are unreachable except from each other get detected and collected. That is a huge advantage over simple reference counting/smart pointers. Commented Jun 17, 2016 at 0:37
  • 6
    +1 The "exceptions" part cannot be stressed enough. The presence of exceptions make the hard and pointless task of manual memory management virtually impossible, and thus manual memory management isn't used in C++. Use RAII. Don't use new outside of constructors/smart pointers, and never use delete outside of a destructor. Commented Jun 17, 2016 at 0:39
  • @Hulk You have a point here ! Although most of my arguments are still valid, GC made a lot of progress. And circular references are indeed difficult to handle with reference counting smart pointers alone. So I edited my answer accordingly, in order to keep a better balance. I also added a reference to an article about possible strategies to mitigate the freeze effect. Commented Jun 17, 2016 at 6:07
  • 3
    +1 The management of resources other than memory does indeed require some extra effort for GC languages because RAII does not work if there is no fine-grained control over when (or if) deletion/finalisation of an object happpens. Special constructs are available in most of these languages (see java's try-with-resources, for example), which can, in combination with compiler warnings, help to get these things right. Commented Jun 17, 2016 at 7:59
  • 1
    Like garbage collector, but one object at a time and without delay (Ok: you need some extra care and weak_ptr to cope with circular references). Reference counting can cascade though. E.g. The last reference to A goes away, but A also has the last reference to B, who has the last reference to C... And you'll have the responsibility to make sure that this method is called when the resources are no longer needed. In every possible branching path through your code, ensuring it is also invoked in case of exceptions. Java and C# have special block statements for this. Commented Jun 17, 2016 at 15:39