To implement automatic memory management for the objects involved in libuv API function calls and user callbacks these uvcc classes are designed with the use of a reference counting technique:
This means that the variables for one of these types are just pointers to object instances created on the heap and have semantics analogous to std::shared_ptr
. A newly created variable produces a new object got instantiated on the heap with initial reference count value equal to one. Copying this variable to another or passing it by value as a function argument just copies the pointer and increases the reference count of the object instance. If the variable goes out of scope of its definition and gets destroyed then the reference count of the object instance is decreased. The count increase/decrease operations are atomic and thread-safe. The object instance by itself gets actually destroyed and the memory allocated for it is automatically released when the last variable referencing the object is destroyed and the reference count becomes zero.