reference count
In my current project, lots of legacy code was designed to use reference count. I know the very basic idea behind it. But I would like to enhance my knowledge for this area since I have开发者_开发百科 to start to use reference count in my daily coding task.
Is there a good book or resource that show me how to use or how to design a good framework/classes that adopt reference count.
Thank you
It's very simple, really.
In your structure you have an integer. When the object is allocated it starts out as 1.
If you're passed one of these objects and you wish to retain it for longer than the lifetime of your function, you bump the reference count by 1. This is known as "holding a reference", and prevents the object from being freed. An example of this would be if you insert it into a collection, or are going to make use of it later, or in another thread. [Nota bene: If you use it in multiple threads you need to make sure the increment/decrement of the count are done with atomic ops.]
If you were responsible for increasing the reference count (or if you were the one who allocated the object in the first place), you decrement the reference count when you don't need the pointer anymore and don't care if it gets freed.
If the reference count reaches zero, you de-allocate.
There are some drawbacks to reference counting:
You can't have data structures with cycles in them. If A has a reference to B and B has a reference to A, A and B's refcount will never go to zero. There are some strategies to avoid this while still having circular structures but they are quite complicated.
In a multi-threaded program, incrementing and decrementing requires atomic Read-Modify-Write instructions, which can be slow. This is especially bad since programs using reference counting will typically change the reference count very frequently, and with atomic ops across multiple threads that's a problem.
It's hard to debug when something goes wrong. You could see a memory leak, double-free or use-after-free, but it won't be clear immediately at the point of the crash who inappropriately modified the refcount.
You can read about smart pointers in boost:
http://www.boost.org/doc/libs/1_46_0/libs/smart_ptr/smart_ptr.htm
Shared pointer there uses reference counting:
http://www.boost.org/doc/libs/1_46_0/libs/smart_ptr/shared_ptr.htm
What you probably have seen is intrusive reference-counting where the class has to be specifically designed so its objects can be reference-counted. You don't need any special precautions if you go for non-intrusive reference-counting instead, as employed by boost::shared_ptr<T>
.
You can read the documentation at Boost or section 20.9.10.2 of the current C++0x standard draft.
To add to the other answers, look at the section in the boost::shared_ptr
manual about custom deleters and managing COM objects. You can have the reference counts automatically decremented when the last shared_ptr
reference disappears, preventing many bugs. You can also use boost::intrusive_ptr
, which is designed for objects with their own reference count management.
精彩评论