Jump to content
  • Advertisement
Sign in to follow this  
cmoney

C++ Garbage Collection

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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[].

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!