Jump to content
  • Advertisement
Sign in to follow this  
Alundra

RefCount pattern ended ?

This topic is 2064 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

Hi all,

RefCount pattern was a lot used in the past.

Is this pattern ended in the modern world we are now ?

Thanks

Share this post


Link to post
Share on other sites
Advertisement

Nice to know about std::shared_ptr, but it's a c++11, maybe it's better actually to have own RefCount class since c++11 is young.

In many newer languages, garbage collection is used instead of reference counting.

It's not the same ? I read on wikipedia that it's based on ref count too.

Or maybe it's the old way who is to use a function to remove all, for example when changing game state ?

Share this post


Link to post
Share on other sites

Nice to know about std::shared_ptr, but it's a c++11, maybe it's better actually to have own RefCount class since c++11 is young.


C++14 is almost here; C++11 isn't that young! Also, std::shared_ptr is basically just a rename of boost::shared_ptr, which has been around for a long time.

That said, I feel that shared_ptr is just a bad idea in practice. Reference counting is fine, but shared_ptr isn't the best way to design an API for it. It was designed to be as slap all through your code as possible, not designed to be easy to debug or for the best performance. Edited by SeanMiddleditch

Share this post


Link to post
Share on other sites

It's not the same ? I read on wikipedia that it's based on ref count too.
Or maybe it's the old way who is to use a function to remove all, for example when changing game state ?


Garbage collection is a group of concepts which sometimes includes ref-counting. Personally, I usually envision the mark-and-sweep algorithm when I hear 'garbage collection' though. Edited by Nypyren

Share this post


Link to post
Share on other sites

In objective C, you have reference counting as well.  Retain and Release.  In the latest version, this is largely replaced with ARC - Automatic Reference Counting, which basically is to wrap everything into a @autoreleasepool {} block.  It is very much alive.

 

It is not always working as it should, though.  Recently I had to wrap the inside of a loop that created and discarded a massive amount of strings into a pool like that of its own.  Otherwise it would eat all my memory.  It reached 5 GB before I terminated the program, even when I discarded most of my strings.  After wrapping into an extra block of autorelease, it spent a steady 20MB for the whole operation.

Share this post


Link to post
Share on other sites

Nice to know about std::shared_ptr, but it's a c++11, maybe it's better actually to have own RefCount class since c++11 is young.


First, as others have said calling C++11 young is a misnomer. Even compilers with suboptimal C++11 support (like MSVC) still implement std::shared_ptr and its friends.
That said, Boost had its own shared_ptr implementation (which is for almost all purposes identical to std::shared_ptr) for years and years already. It also contains the often forgotten intrusive_ptr which avoids several of the problems pointed out with (std|boost)::shared_ptr.

Share this post


Link to post
Share on other sites
In many newer languages, garbage collection is used instead of reference counting.

It's not the same ? I read on wikipedia that it's based on ref count too.

 

Garbage collection could be implemented using ref counting, but usually it isn't. Ref counting has a performance overhead, and it has problems with cyclic references.

 

Actually, when object lifetime is managed by ref counts, the object is removed when the count reaches 0, so there is no garbage, and no need for collection. I wouldn't call this "garbage collection".

Share this post


Link to post
Share on other sites

Actually, when object lifetime is managed by ref counts, the object is removed when the count reaches 0, so there is no garbage, and no need for collection. I wouldn't call this "garbage collection".

One caveat - in a good thread-safe ref-counting system (not shared_ptr, which pays a hefty performance cost in exchange for partial thread-safety...), any decremented object is potentially garbage, to be inspected at a point in the future ;)

Share this post


Link to post
Share on other sites
First, as others have said calling C++11 young is a misnomer.

Yea, it's true, I said that because if I'm not wrong not all compiler has features of c++11 actually so to be cross having own class is better.

Is it possible to clarify things about the ref count pattern ?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!