Cocos2Dx memory management Encapsulation

Source: Internet
Author: User

I feel like Cocos2Dx, the architecture is clear and reasonable, but the memory management mechanism is really strange to me.

Every time you create an object, you use the creatXXX method. If you want to keep the object pointer, you still need to retain it. Remember to release it when you destroy the object, which is really annoying.

I tried to store data in a way similar to smart pointers.

 

Let's talk about the original Cocos2Dx Automatic Memory Management Mechanism:

In the Cocos2Dx CCObject class, there are two functions, retain, release, and a variable m_uReference, used to control the reference count. When one retain reference count is called, when release is called, the reference count is reduced by one. If the reference count is 0 at this time, the delete function is also called, and the autorelease function is called in the static factory method (CCXXX :: create (). This method is used to put the object into the memory management pool (CCPoolManage) and clear it at the end of each frame. The reference count is 1 (only available in the memory management pool).

In the official example, we can see that developers do not want to manually manage the memory (new), but if automatic management is required, they must remember retain and release every moment. Otherwise, either the wild pointer or the memory will leak, if you do not pay attention to this problem at the end of the project, you may be unable to return to the day.

So I wrote a template similar to a smart pointer to solve this problem. No nonsense. Go to the Code:

 

[Cpp]
# Ifndef _ CCPTR_H _
# Define _ CCPTR_H _
NS_CC_BEGIN
 
Template <class _ Ty>
Class CCPtr
{
Public:
Typedef CCPtr <_ Ty> _ Myt;
 
Explicit CCPtr (_ Ty * _ Ptr = 0)
: _ Myptr (_ Ptr)
{
CC_SAFE_RETAIN (_ Myptr );
}
 
Explicit CCPtr (_ Myt const & _ Ref)
: _ Myptr (_ Ref. get ())
{
CC_SAFE_RETAIN (_ Myptr );
}
 
_ Myt & operator = (_ Myt & _ Right)
{
_ Ty * ptr = _ Right. get ();
CC_SAFE_RETAIN (ptr );
CC_SAFE_RELEASE (_ Myptr );
_ Myptr = ptr;
 
Return (* this );
}
 
_ Myt & operator = (_ Ty * ptr)
{
CC_SAFE_RETAIN (ptr );
CC_SAFE_RELEASE (_ Myptr );
_ Myptr = ptr;
 
Return (* this );
}
 
Template <class _ Other>
Operator CCPtr <_ Other> ()
{
Return (CCPtr <_ Other> (* this ));
}
 
Template <class _ Other>
_ Myt & operator = (CCPtr <_ Other> & _ Right)
{
_ Other * ptr = _ Right. get ();
CC_SAFE_RETAIN (ptr );
CC_SAFE_RELEASE (_ Myptr );
_ Myptr = ptr;
 
Return (* this );
}
 
Template <class _ Other>
CCPtr (CCPtr <_ Other> & _ Right)
: _ Myptr (_ Right. get ())
{
CC_SAFE_RETAIN (_ Myptr );
}
 
~ CCPtr ()
{
CC_SAFE_RELEASE (_ Myptr );
}
 
_ Ty & operator * () const
{
Return (* get ());
}
 
_ Ty * operator-> () const
{
Return (get ());
}
 
_ Ty * get () const
{
Return (_ Myptr );
}
 
Private:
_ Ty * _ Myptr; // the wrapped object pointer
};
 
 
Template <class T, class U> inline bool operator = (CCPtr <T> & a, CCPtr <U> & B)
{
Return a. get () = B. get ();
}
 
Template <class T, class U> inline bool operator! = (CCPtr <T> & a, CCPtr <U> & B)
{
Return a. get ()! = B. get ();
}
 
Typedef CCPtr <CCAction> CCActionPtr;
Typedef CCPtr <CCActionInterval> CCActionIntervalPtr;
Typedef CCPtr <CCActionCamera> CCActionCameraPtr;
Typedef CCPtr <CCActionManager> CCActionManagerPtr;
Typedef CCPtr <CCActionEase> CCActionEasePtr;
Typedef CCPtr <CCPageTurn3D> CCActionPageTurn3DPtr;
Typedef CCPtr <CCGridAction> CCActionGridPtr;
Typedef CCPtr <CCProgressTimer> CCActionProgressTimerPtr;
Typedef CCPtr <CCGrid3D> CCActionGrid3DPtr;
Typedef CCPtr <CCActionInstant> CCActionInstantPtr;
Typedef CCPtr <CCActionTween> CCActionTweenPtr;
Typedef CCPtr <CCAffineTransform> CCAffineTransformPtr;
Typedef CCPtr <CCDictionary> CCDictionaryPtr;
Typedef CCPtr <CCObject> CCObjectPtr;
Typedef CCPtr <CCArray> CCArrayPtr;
Typedef CCPtr <CCSet> CCSetPtr;
Typedef CCPtr <CCAutoreleasePool> CCAutoreleasePoolPtr;
Typedef CCPtr <CCInteger> CCIntegerPtr;
Typedef CCPtr <CCFloat> CCFloatPtr;
Typedef CCPtr <CCDouble> CCDoublePtr;
Typedef CCPtr <CCBool> CCBoolPtr;
Typedef CCPtr <CCString> CCStringPtr;
Typedef CCPtr <CCZone> CCZonePtr;
Typedef CCPtr <CCAnimation> CCAnimationPtr;
Typedef CCPtr <CCNode> CCNodePtr;
// Add other typedef
//...
NS_CC_END
 
# Endif // _ CCPTR_H _

# Ifndef _ CCPTR_H _
# Define _ CCPTR_H _
NS_CC_BEGIN

Template <class _ Ty>
Class CCPtr
{
Public:
Typedef CCPtr <_ Ty> _ Myt;

Explicit CCPtr (_ Ty * _ Ptr = 0)
: _ Myptr (_ Ptr)
{
CC_SAFE_RETAIN (_ Myptr );
}

Explicit CCPtr (_ Myt const & _ Ref)
: _ Myptr (_ Ref. get ())
{
CC_SAFE_RETAIN (_ Myptr );
}

_ Myt & operator = (_ Myt & _ Right)
{
_ Ty * ptr = _ Right. get ();
CC_SAFE_RETAIN (ptr );
CC_SAFE_RELEASE (_ Myptr );
_ Myptr = ptr;

Return (* this );
}

_ Myt & operator = (_ Ty * ptr)
{
CC_SAFE_RETAIN (ptr );
CC_SAFE_RELEASE (_ Myptr );
_ Myptr = ptr;

Return (* this );
}

Template <class _ Other>
Operator CCPtr <_ Other> ()
{
Return (CCPtr <_ Other> (* this ));
}

Template <class _ Other>
_ Myt & operator = (CCPtr <_ Other> & _ Right)
{
_ Other * ptr = _ Right. get ();
CC_SAFE_RETAIN (ptr );
CC_SAFE_RELEASE (_ Myptr );
_ Myptr = ptr;

Return (* this );
}

Template <class _ Other>
CCPtr (CCPtr <_ Other> & _ Right)
: _ Myptr (_ Right. get ())
{
CC_SAFE_RETAIN (_ Myptr );
}

~ CCPtr ()
{
CC_SAFE_RELEASE (_ Myptr );
}

_ Ty & operator * () const
{
Return (* get ());
}

_ Ty * operator-> () const
{
Return (get ());
}

_ Ty * get () const
{
Return (_ Myptr );
}

Private:
_ Ty * _ Myptr; // the wrapped object pointer
};


Template <class T, class U> inline bool operator = (CCPtr <T> & a, CCPtr <U> & B)
{
Return a. get () = B. get ();
}

Template <class T, class U> inline bool operator! = (CCPtr <T> & a, CCPtr <U> & B)
{
Return a. get ()! = B. get ();
}

Typedef CCPtr <CCAction> CCActionPtr;
Typedef CCPtr <CCActionInterval> CCActionIntervalPtr;
Typedef CCPtr <CCActionCamera> CCActionCameraPtr;
Typedef CCPtr <CCActionManager> CCActionManagerPtr;
Typedef CCPtr <CCActionEase> CCActionEasePtr;
Typedef CCPtr <CCPageTurn3D> CCActionPageTurn3DPtr;
Typedef CCPtr <CCGridAction> CCActionGridPtr;
Typedef CCPtr <CCProgressTimer> CCActionProgressTimerPtr;
Typedef CCPtr <CCGrid3D> CCActionGrid3DPtr;
Typedef CCPtr <CCActionInstant> CCActionInstantPtr;
Typedef CCPtr <CCActionTween> CCActionTweenPtr;
Typedef CCPtr <CCAffineTransform> CCAffineTransformPtr;
Typedef CCPtr <CCDictionary> CCDictionaryPtr;
Typedef CCPtr <CCObject> CCObjectPtr;
Typedef CCPtr <CCArray> CCArrayPtr;
Typedef CCPtr <CCSet> CCSetPtr;
Typedef CCPtr <CCAutoreleasePool> CCAutoreleasePoolPtr;
Typedef CCPtr <CCInteger> CCIntegerPtr;
Typedef CCPtr <CCFloat> CCFloatPtr;
Typedef CCPtr <CCDouble> CCDoublePtr;
Typedef CCPtr <CCBool> CCBoolPtr;
Typedef CCPtr <CCString> CCStringPtr;
Typedef CCPtr <CCZone> CCZonePtr;
Typedef CCPtr <CCAnimation> CCAnimationPtr;
Typedef CCPtr <CCNode> CCNodePtr;
// Add other typedef
//...
NS_CC_END

# Endif/_ CCPTR_H _ because there are too many classes involved, you have not added all the class templates. Please add them as needed.

The usage of CCNode is used as an example.

We define a CCNodePtr member m_testNode in the Class header file.

Then assign a value to it where it is used. Pai_^

M_testNode = CCNode: create ();

Then you can perform the operations you need

Example: m_testNode-> setPositionX (0 );

The usage is the same as that of CCNode,

You don't need to care about retain and release, but you need to note that when passing parameters, the reference and pointer of CCPtr that cannot be passed can only pass objects. The usage is similar to the smart pointer usage in boost. ^_^

 

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.