RefPtr and RefPtrConst[C++] template <class T>
Remarks RefCountable and IRefCountable. In FlowSshCpp, all objects derived from RefCountable and IRefCountable are reference counted objects. The following rules outline the usage guidelines for them:
RefPtr and RefPtrConst. The declaration at the beginning of this document clearly shows that RefPtr and RefPtrConst are related and provide virtually the same functionality. What is important to understand is that none of their constructors implies an AddRef call. Similarly, if you try to assign a raw ordinary pointer to RefPtr or RefPtrConst, AddRef is not called either. The following example demonstrates this. function Erroneous()
In the sample the reference count for our client object remains 1 all the time. Nevertheless, the object is assigned to two reference pointers. As soon as these pointers go out of scope, they both try to Release the object and thus delete it from memory as the reference count drops to zero. Obviously, deleting the same object twice results in an access violation crash. To fix the code we could manually increase the reference count via AddRef. Alternatively, we can conveniently assign the first refPtr1 to refPtr2 directly. In this case, the reference count is implicitly increased and handled correctly. The next example demonstrates this. function Working()
As the function goes out of scope first refPtr2 is destructed; the destructor of refPtr2 calls Release on the client, dropping its reference count to 1. Right thereafter refPtr1 is destructed; at this point the reference count of the client drops to zero and it is deleted from memory. Accessors. There are several accessors available for RefPtr and RefPtrConst. Get returns a raw pointer and GetRef a reference to the wrapped object. Though, most of the times you will simply use the operator->. It lets you access the wrapped object as if using an ordinary raw pointer. function Accessors()
RefPtr vs. RefPtrConst. You may wonder by now if there is a difference between RefPtr and RefPtrConst after all? In fact, the only difference is that RefPtrConst points to an object with constant content (imagine the "Client const* rawPtr;" as an equivalent). In other words, with RefPtrConst you can only invoke const methods of the wrapped object. |