Garbage Collection library C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Garbage Collection library C++

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Garbage Collection library C++

    I am looking for a garbage collection library for C++ , and some documentation on how to use , Google search produced one by HP , however not sure how to use it.

    IF some has better suggestion like smart pointer then please feel free to comment ,

    I am currently working on the project ( developed by some one else ) it involves allocating lot of object pointers in vector array , and keeping track of all of them , I was essentially looking for a Java type solution ( if one exists ) to when an Object is no longer referenced by a pointer it get deleted automatically as oppose to manually deleting it.

    I guess it sames on a lot of typing and careless errors.

    I have heard of smart pointers however not sure how to use them.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Garbage Collection library C++

    Quote Originally Posted by aamir121a View Post
    I was essentially looking for a Java type solution ( if one exists ) to when an Object is no longer referenced by a pointer it get deleted automatically as oppose to manually deleting it.
    std::shared_ptr<T> or boost::shared_ptr<T>.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Garbage Collection library C++

    Thanks Paul , could you please provide example on how this is used

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Garbage Collection library C++

    Quote Originally Posted by aamir121a View Post
    Thanks Paul , could you please provide example on how this is used
    Why not do a google search? The shared_ptr is now a part of standard C++, and explanations of how to use it are in abundance.
    Code:
    #include <memory>
    
    struct foo
    { 
        int x;
        foo() : x(10) {}
    };
    
    typedef std::shared_ptr<foo> foo_ptr; // or std::tr1::shared_ptr if using VC 2008
    
    int main()
    {
       foo_ptr f;  // shared pointer not pointing to anything
    
       // create a local scope for demo purposes
       {
           foo_ptr f1(new foo());  // create shared pointer and point it to a foo object, reference count is 1
    
           f = f1;  // now we have two pointers pointing to a foo, f and f1, so reference count is now 2
    
        }  // f1 goes out of scope -- reference count is decremented and is now 1.
    
    }   // Now f has gone out of scope.  reference count is decremented 
        // and is now 0, so "delete" will be called automatically.
    In other words, you are guaranteed to have the memory cleaned up when the last pointer pointing to the dynamically created object goes out of scope.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Garbage Collection library C++

    Quote Originally Posted by aamir121a View Post
    I am looking for a garbage collection library for C++ ,
    I've never used it myself but the garbage collector you most often hear about is this,

    http://www.hpl.hp.com/personal/Hans_Boehm/gc/

    Smart pointers are a good alternative I think. But if you have a Java background there's a limitation you should know about. Smart pointers cannot handle circular chains. If an object holds a smart pointer to an object this object may not hold a smart pointer back, not directly nor indirectly.

    Also, but this is secondary, since smart pointers make indirect accesses and since they keep a counter updated they're somewhat slower than ordinary bald pointers. I'm using smart pointers quite frequently and I've had an issue with this once only.

    When I first graduated from Java to C++ I used smart pointers everywhere because they felt so familiar. Today I'm limiting their use mostly to publicly visible objects lacking an obvious owner. I find manual object management to be perfectly fine in implementation style code.

    Finally I would like to recommend Intel TBB. It's not only great for writing multi-core programs, it also comes with an efficient memory allocation replacement,

    http://threadingbuildingblocks.org/
    Last edited by nuzzle; September 18th, 2011 at 03:09 AM.

  6. #6
    Join Date
    Oct 2008
    Posts
    1,077

    Re: Garbage Collection library C++

    Quote Originally Posted by nuzzle View Post
    But if you have a Java background there's a limitation you should know about. Smart pointers cannot handle circular chains. If an object holds a smart pointer to an object this object may not hold a smart pointer back, not directly nor indirectly.
    regarding reference counted smart pointers, this issue can be solved by using weak_ptr in conjunction with shared_ptr, so the claim that "Smart pointers cannot handle circular chains" is a bit too strong; moreover, there's also unique_ptr which forbids circular references from the beginning ( note that the OP speaks of "object pointers in vector array", so, he doesn't necessarily need a shared reference semantics ... )

    anyway, I agree that a liberal use of shared_ptr as a blind replacement of object pointers/references is the wrong thing to do, although I cannot imagine a different reason for this to be assumed other then coming from a Java background ...

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Garbage Collection library C++

    Quote Originally Posted by superbonzo View Post
    so the claim that "Smart pointers cannot handle circular chains" is a bit too strong;
    Maybe, but the circular reference problem still requires special attention and a work-around.

    If you naively use smart pointers in the belief they work just like garbage collection ala Java you're in for a surprise.
    Last edited by nuzzle; September 18th, 2011 at 04:07 AM.

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Garbage Collection library C++

    Quote Originally Posted by superbonzo View Post
    anyway, I agree that a liberal use of shared_ptr as a blind replacement of object pointers/references is the wrong thing to do, although I cannot imagine a different reason for this to be assumed other then coming from a Java background ...
    Sure, having a Java background is the ultimate liability for a C++ programmer.

    Although reference counting smart pointers is an acceptable solution it doesn't beat full garbage collection. I wish C++ was extended to provide it. The C++/CLI language is a nice proof of concept. But it must be in the good old C++ tradition of course; Using it is voluntary and you don't pay if you don't use it.
    Last edited by nuzzle; September 18th, 2011 at 04:29 AM.

  9. #9
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Garbage Collection library C++

    IF some has better suggestion like smart pointer then please feel free to comment ,
    Just clean up your own memory. How hard is that ?

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: Garbage Collection library C++

    Quote Originally Posted by Skizmo View Post
    Just clean up your own memory. How hard is that ?
    It varies but a few beers usually do the trick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center