Custom Garbage Collection

Member
Posts: 100
Joined: 2006.05
Post: #1
As much as I love the garbage collection and OOP concepts in Objective-C(++), I will be writing the majority of my game engine in C++ (with Cocoa front end) for portability. Inspired by the Enginuity Part II article, I am considering implementing my own reference counting system and smart pointers. I was wondering what the community has to say on the issue. Is it worth it? Is it more trouble/overhead than it's worth? Is there an even better approach? I'm not sure exactly how big my project will turn out, but I want to try and close up all the loose ends that I can so I have a powerful, expandable, and portable game engine (end yes, I am sort of bent on doing most of the coding myself). Thanks.

- Nevada
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
Don't. Instead, look into boost's smart pointers. They already work, and a lot of very smart people have worked out the kinks.

I use smart pointers *everywhere*, and by god, they work. It's 90% as good as real GC, but with the knowledge that you still have RAII and proper calling of destructors.
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #3
Speaking of boost, how do you install it? I tried ./configure but it said permission denied, so when I tried running it with sudo, it said command not found. Any trick to install on Mac OS X?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
chmod +x configure might help Rasp

You can garbage-collect C++ if you want. Look into the Boehm-Demers-Weiser conservative garbage collector for C and C++.
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #5
Hmm, not I get 'bad interpreter: no such file or or directory
Quote this message in a reply
Member
Posts: 100
Joined: 2006.05
Post: #6
Well, the boost library looks pretty powerful. I have never used one of these large C++ libraries before and I am wondering if I will have to force my users to get it as well or if it will bloat my game having to include it. Their smart pointers are a bit different than I had expected. Do their smart pointers avoid the use of retain(), release(), collectGarbage() altogether?

A bit off topic, but what do you think about coding a custom kernal?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #7
bronxbomber92: I just installed it a few days ago. It took a looong time to build the libraries, like 45 minutes or more. I don't know much about command line operations, so I can't help other than to say that `It worked for me!'.

Nevada: There are two parts to Boost: The libs and the headers. From what I understand, most of the code is in the headers. You can compile programs that don't even touch the libs -- just read the getting started stuff at the Boost website. Either way, you won't need your users to install it since all the relevant code compiles into your program(s). You just have to have it installed on your dev machine. It's big and it's powerful! I'm not a C++ advocate by any stretch of the imagination, but if I were, I would definitely recommend trying out Boost. Binary bloat doesn't seem to be too much of a problem from what I've seen. [flamebait]But as far as code bloat, you're programming in C++, what do you expect? Bloat is just a fact of life to C++ programmers.[/flamebait]
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Boost smart pointers you should generally just have to use them instead of raw pointers, and they'll handle reference counting behind the scenes.

Anything where reference counting doesn't work (cyclical data structures) you'll have to be careful, or you'll leak.

with boehm-gc you can either replace the global new/delete operators with ones that call GC_malloc and GC_free, or you can inherit from the "GC" class, which does that for just it and its subclasses.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #9
OneSadCookie Wrote:Boost smart pointers you should generally just have to use them instead of raw pointers, and they'll handle reference counting behind the scenes.

Exactly, a boost smart pointer "retains" on creation/assignment and releases on destruction. This works great, particularly, when you use exceptions for error handling.

OneSadCookie Wrote:Anything where reference counting doesn't work (cyclical data structures) you'll have to be careful, or you'll leak.

This has only cropped up for me when creating trees or doubly-linked lists. For those cases, boost provides a "weak" smart pointer which I use to point to the parent. It's easy to use. The weak_ptr provides a function called "lock" which attempts to create a shared_ptr from the weak_ptr.

Code:
void MyTreeNode::foo( void )
{
   // _parent is a weak_ptr< MyTreeNode >
   if ( shared_ptr< MyTreeNode > p = _parent.lock() )
   {
      p->whatever();
   }
}

The other situation where I use weak_ptr is for, for example, keeping a pointer to the "World" instance my objects live in. Since the World "owns" everything by keeping std::set instances of shared_ptr< Entity >, if the Entities had shared_ptr< World > they'd never get freed. So, yeah, it's not as "easy" as real GC, but it's pretty damn straightforward so long as you think a little about what's going on.

One important thing to know, which took me a while to figure out, is the situation where a class wants to dole out a shared_ptr of 'this'. boost provides a class boost::enable_shared_from_this< T > which you can derive from. This is a lifesaver.


OneSadCookie Wrote:with boehm-gc you can either replace the global new/delete operators with ones that call GC_malloc and GC_free, or you can inherit from the "GC" class, which does that for just it and its subclasses.

This scares me a little, but frankly, it's pretty appealing. That being said, I'd rather just run with a language designed for GC in the first place, like D.
Quote this message in a reply
Member
Posts: 100
Joined: 2006.05
Post: #10
While we're on the subject, are memory pools worth implementing (or even using)?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #11
I've had good luck with pooling in Chipmunk. In particular I pool the hashbins, as they are constantly getting allocated and deallocated. In this case it worked out well because they already essentially were linked list nodes, so pooling them was a simple matter of throwing unused bins onto a list. It did help performance a few percent.

Considering that it was only a few percent, I wouldn't bother doing anything more complicated with pooling I think.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
mattg
Unregistered
 
Post: #12
Another garbage collector is available for your consideration at http://hnxgc.harnixworld.com

It is accurate, pauseless with deterministic reclamation, efficient and portable.
Quote this message in a reply
Post Reply