The implementation of traditional smart pointers mentioned in C ++ Primer:
The essence of a smart pointer is to allocate a pointer to a resource object on the stack. when an object is out of scope, its destructor is automatically called. Modify the definition of the Destructor: when the reference count of the current object is not 0, the count will be reduced by one. If the reference count after the reduction is 0, the current object will be deleted:
The common implementation of smart pointers and reference counts:
Common memory management methods include intelligent pointer and reference counting technology. The following discusses the realization of intelligent pointer in cocos2d-x, Cef and C ++ 11 respectively:
Implementation of reference count in Cocos2d-x: 1. How to Implement reference count and memory management base class in ccobject m_ureference save reference count value:
class CCObject{public: // object id, CCScriptSupport need public m_uID unsigned int m_uID; // Lua reference id int m_nLuaID;protected: // count of references unsigned int m_uReference; // is the object autoreleased bool m_bManaged; public: CCObject(void); virtual ~CCObject(void); void release(void); void retain(void); CCObject* autorelease(void); CCObject* copy(void); bool isSingleReference(void); unsigned int retainCount(void); virtual bool isEqual(const CCObject* pObject); virtual void update(float dt) {CC_UNUSED_PARAM(dt);}; friend class CCAutoreleasePool;};2. How to create an object
3. How to reclaim memory