C ++ memory management-reference count

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.