Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm interested in a rough measure (percentage-wise) of how many use garbage collection, such as the Boehm collector, with standard C++.

Under what circumstances would you use garbage collection, and when wouldn't you, with C++? Are there other alternatives?

share|improve this question
    
See the subjective guidelines; in particular, multiple choice questions aren't good. –  Roger Pate Nov 1 '10 at 16:22
    
@Roger: I hope for a bunch of very non-subjective, factual answers. I think it would be perhaps be premature to ask for experiences, typical application areas, so on. Although that may fall out of this? –  Alf P. Steinbach Nov 1 '10 at 16:26
add comment

2 Answers

I have, but at least for me it seemed to cause more problems than it solved. Using GC with C++ basically makes it much more like other languages that use GC -- in particular, you lose the deterministic destruction of objects that's (more or less) necessary for RAII to work correctly. This, in turn, means that when (for example) you want to destroy something deterministically, you need to do so explicitly like you do in something like Java. IMO, that's somewhat clumsy in Java -- but much more so in C++, since it lacks a finally clause for the purpose.

Under normal circumstances, you can use a dtor and RAII to provide all the capabilities of a finally clause, but the whole problem here is that with GC you no longer get that. That means almost any time you want to destroy something deterministically, you need to do so explicitly and throw (pun noted by not intended) in extra try/catch clauses to ensure that it happens no matter how you exit the area where you needed the resource.

In the end, it comes down to one thing: at least for me, GC accomplishes next to nothing, because intelligent use of container classes has basically eliminated memory management problems without it. RAII allows me to manage memory and virtually all other resources cleanly. Using GC means 1) the memory isn't managed as well (memory footprint is generally considerably larger), and 2) I have to go back to managing other resources manually.

share|improve this answer
    
Eh, you're doing it wrong. You don't lose deterministic destruction; local objects still go out of scope when functions return etc. "GC" merely simulates infinite memory, that's true - but that's true in all GC languages. –  MSalters Nov 25 '10 at 11:01
add comment

I use garbage collection with smart pointers (Boost's shared_ptr). It's mostly used for large objects that are shared throughout the system. It encourages passing the pointer around and ensures the object is destroyed at some point.

I also use the shared pointers with a publish/subscribe type system. Mostly because I don't know how many handlers are going to have access to the same event pointer and I don't have a definite lifetime for that event.

I will admit that using the shared pointers for the objects has caused more trouble than help at certain points. Mostly because these objects have a clear and definite time of destruction/creation and there's no need for them to be garbage collected. In fact, it keeps hiding errors where an object is holding onto a subsystem for too long and the subsystems are being destroyed in the incorrect order.

I have never used a garbage collector similar to the one that's used for Java. Didn't know those existed.

share|improve this answer
    
Thanks for your response. I added a link to the Wikipedia article on the Boehm collector, in the question. Cheers, –  Alf P. Steinbach Nov 1 '10 at 17:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.