Cocos2d Memory Management

Source: Internet
Author: User

Cocos2d Memory Management

-- Don't be afraid. Be brave enough to take the first step. Come on !!!!! --


This chapter introduces memory management in cocos2d.


1. All Node classes in cocos2d inherit from Ref. Ref has the concept of reference count, as follows:

Class TestRef: public Ref {

Public:

TestRef (){}

Virtual ~ TestRef (){}

};

New TestRef -- in this case, _ referenceCount = 1 in class Ref

Retain -- _ referenceCount ++

Release -- _ referenceCount --, if _ referenceCount = 0, describe this class

AutoRelease -- puts this in the PoolManager memory pool. When PoolManager recycles the class, it recycles the class and analyzes the structure.


Example 1

Auto p = new TestRef (); _ referenceCount = 1

P-> retain (); _ referenceCount = 2

P-> release (); _ referenceCount = 1

P-> release (); _ referenceCount = 0, destructor p


Example 2

Auto p = new TestRef (); _ referenceCount = 1

P-> autoRelease (); When PoolManager recycles the class, it recycles the class and analyzes the structure.


This is the memory management in cocos2d.

If we are using some sprite, layer, and scene before and after addChild, do not call its retain or release if it is not a special need, because after creation, this component calls autoRelease and releases its child nodes after its parent node is released.


2. Do not save the member variables of the non-Node class in the inherited class of the component. What does that mean? Here is a brief description,

Class TestLayer: public layer {

Public:

Virtual ~ TestLayer (){

T-> release ();

}

CREATE_FUNC (Fight );

Private:

TestRef * t;

};


Bool TestLayer: init (){

If (! Layer: init ())

Return false;

T = new TestRef ();

Return true;

}

Add the current TestLayer to Scene and exit the program. Both Scene and TestLayer will be destructed. In the TestLayer destructor, We need to describe TestRef, but in the breakpoint of the destructor of TestLayer, I can see that t is an invalid pointer, in addition, TestRef's destructor has not been called, and "memory leak" is suspected as follows: for personal reference only. If anyone knows the specific answer, please let us know. Thank you ~~~~

Before TestLayer destructor, all its child components will be destructed, and all its member variables will be reset to a wild pointer. What is the reason, memory leakage occurs when the program exits. Based on this, you can determine whether the memory is leaked or where the memory is leaked.


Conclusion:

1. cocos2d memory management effectively solves the habit of forgetting to release memory for some children's shoes. Of course, this habit is not good.

2. Do not save any member variable objects in the inherited class of the component.



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.