views:

480

answers:

6

What garbage collectors are there available for C++? Are you using any of them? With what results?

+5  A: 

The only one I've heard of personally is the Boehm garbage collector I'm sure others exist, but I've not dealt with them (or looked for them either).

Harper Shelby
+7  A: 

The Boost library includes some shared_ptr stuff that basically acts as a reference counting garbage collector. If you embrace the RAII principle of C++ design, that and auto_ptr will fill your need for a "garbage collector".

rmeador
Not if you want to be able to model cyclical references.
Daniel Earwicker
+5  A: 

Several C++ GC are listed on wikipedia.

However, I don't use any, RAII is also my friend.

Luc Hermitte
+5  A: 

The Boehm garbage collector is pretty good for C, but tricky to use under C++. Check the "C++ interface" section at http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcinterface.html.

My opinion is that if you need garbage collection, choose a langage that has it built-in.

The best general solution for C++ is shared pointers (from boost for example) with you dealing with circular dependencies. There are two things you can do: 1. design the thing with no circular dependencies 2. design the thing with a 'linch-pin' that breaks the circle to allow reclamation of the objects

Either you deal with real bad, convoluted, hard to debug problems with a garbage collector for C++ or you deal with the simpler classical problem of freeing your objects when you are done with them.

Philippe Payant
+3  A: 

There's always, ahem: C++/CLI -- C++ for the .NET Framework. Pretty good garbage collection there. :p

Although, to be honest, with all the syntactic sugar they put in there, you could almost consider it a whole new language that just happens to work with C/C++ fairly well.

If you're not married to C++ as a language, you could also look into D, which compiles to native code like C++ (and unlike C++/CLI) but also has garbage collection.

Randolpho
+1  A: 

You can find several implementations here. I have never tried any of them and in general I find a non-deterministic GC causing more harm than good.

Nemanja Trifunovic