Managing memory using reference counting
When working in the native code environment, every memory allocation event is handled by the developer. Tracking all the allocations in a multithreaded environment becomes notoriously difficult. The C++ language provides a way to avoid manual object deallocation using smart pointers. Since we are developing mobile applications, we cannot afford to use the whole Boost library just to include smart pointers.
Note
You can use the Boost library with Android NDK. The main two reasons we avoid it in our small examples are as follows: a drastically increased compilation time and the desire for showing how basic things can be implemented yourself. If your project already includes Boost, you are advised to use smart pointers from that library. The compilation is straightforward and does not require special steps for porting.
Getting ready
We need a simple intrusive counter to be embedded into all of our reference-countered classes. Here, we provide a lightweight...