C++ Garbage Collection

Started by
4 comments, last by SirLuthor 18 years, 10 months ago
Hello! I'm writing (or trying to write) a fairly sophisticated 3D Game Engine. I want to implement a garbage collection scheme. I've read some material and familiarized myself with reference counting, mark-sweep algorithms, etc. I'm pretty sure I want to use a mark-sweep scheme, but I'm confused on how to implement such a system in C++. I've thought of making a method each class must overload to return all of that object's pointers, but there must be a more practical solution. Something more dynamic. I guess my question is: how do I implement mark-sweep garbage collection in C++. It is possible? [looksaround] How do some commercial products handle GC?
Advertisement
Hans Boehm's Garbage Collector

Though you need to decide whether you need full garbage collection, or whether a smart pointer class is enough for you (reference counted smart pointers are a form of garbage collection).

Quote:I've thought of making a method each class must overload to return all of that object's pointers,


If you override a class (Foo) operator new, then any dynamic allocation (new Foo) will go through the overriden operator new. That's how your GC would intercept allocations. Idem for operator delete, operator new[] and operator delete[].
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks, Fruny. But perhaps wasn't specific enough. I know how to intercept object allocations. I'm puzzled at how to check whether an object is referenced or not, without using reference counts. I'm familiar with the mark-sweep algorithm, but how exactly do I make C++ do it?
Try the link he posted...
After a quick glance, the link didn't seem to have much that can help me, but I'll take a better look at it later.
Quote:Original post by Fruny
Hans Boehm's Garbage Collector

Though you need to decide whether you need full garbage collection, or whether a smart pointer class is enough for you (reference counted smart pointers are a form of garbage collection).

Quote:I've thought of making a method each class must overload to return all of that object's pointers,


If you override a class (Foo) operator new, then any dynamic allocation (new Foo) will go through the overriden operator new. That's how your GC would intercept allocations. Idem for operator delete, operator new[] and operator delete[].

Heheh, did mister Fruny forget to close his tele-type elements? [grin]
<tt>operator new<tt>
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!

This topic is closed to new replies.

Advertisement