C++ Garbage Collection
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?
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).
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[].
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[].
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?
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>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement