RefCount pattern ended ?

Started by
8 comments, last by Alundra 10 years, 1 month ago

Hi all,

RefCount pattern was a lot used in the past.

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

Thanks

Advertisement

In C++, everyone used to implement this themselves. Now it's a standard part of the language, called std::shared_ptr!

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

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 ?

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.

Sean Middleditch – Game Systems Engineer – Join my team!

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.

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.

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.
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".

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 ;)
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 ?

This topic is closed to new replies.

Advertisement