Cocos2d-x development in C ++ memory management, cocos2d-x Memory Management
Since the beginning does not introduce C ++ language, C ++ memory management of course did not have any instructions, in order to master the memory management mechanism in the Cocos2d-x, it is necessary to first understand some knowledge of C ++ memory management.
C ++ memory management is very complicated. If you fully introduce it systematically, it may take a book to explain it clearly. Here we will only introduce the most basic usage of C ++ memory management.
Memory Allocation Area
Two steps are required to create an object: Step 1: allocate memory for the object, and Step 2: Call the constructor to initialize the memory. In the first step, when the object is allocated memory, we can select several different allocation regions, which are as follows:
Stack region allocation. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited. It is automatically assigned and released by the processor to store function parameter values and local variable values. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released.
Heap region allocation. Allocated from the stack, also known as dynamic memory allocation. Released by developers. if released, the operating system recycles the program when it ends. When a program runs, it uses malloc or new to apply for any amount of memory. Developers are responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by the developers. It is very flexible to use, but it has the most problems.
In the static storage area. This memory space exists throughout the entire running period of the program and is allocated when the program is compiled. It can allocate global variables and static variables.
Dynamic Memory Allocation
Dynamic memory allocation is the most flexible, but there are also many problems. We will focus on dynamic memory allocation. Dynamic Memory uses malloc or new to allocate memory, and uses free or delete to release memory. Among them, malloc and free are paired, while new and delete are paired.
1. Use malloc and free
Malloc and free are standard library functions in C/C ++, mainly used in C. Using malloc to create an object does not automatically call the constructor to initialize the memory. Releasing objects with free will not automatically call the destructor to clear the memory.
The code for allocating and releasing memory using malloc and free is as follows:
# Include <iostream> using namespace std; class MyObject {public: MyObject () {① cout <"call constructor." <endl ;}~ MyObject () {② cout <"call destructor. "<endl;} void initialize () {③ cout <" call initialization. "<endl;} void destroy () {④ cout <" call destroy. "<endl ;}}; int main () {MyObject * obj = (MyObject *) malloc (sizeof (MyObject )); // apply for dynamic memory ⑤ obj-> initialize (); ⑥ // TODOobj-> destroy (); 7free (obj); required obj = NULL; return 0 ;}
The above Code creates a declared MyObject class. The first line of code declares the constructor, and the second line of code declares the destructor. The Code in line ③ declares that the initialization function void initialize () cannot call the constructor when malloc is used to allocate memory. The object is initialized by calling this function. The Code in line ④ declares the void destroy () function to clear some resources of the object by calling this function when the object memory is released using free.
⑤ ~ The callback line calls the MyObject class for testing. The fifth line of code MyObject * obj = (MyObject *) malloc (sizeof (MyObject) is to use the malloc function to allocate memory, to use this function, you must specify the object length, and the return value of the malloc function is void *. Since C ++ does not allow void * to be assigned to other pointers, forced type conversion is required. Because the constructor cannot be called explicitly, the Code in line 6 is required to initialize the object.
The first line of code free (obj) is to release the obj object memory. Before releasing the object memory, we call the code obj-> destroy () in line 7 before releasing the object memory to clear some resources of the object, it serves as a destructor. But the real destructor ~ MyObject () is not called.
The running result is as follows:
Call initialization.
Call destroy.
2. Use new and delete
Unlike malloc and free, new and delete are not function libraries, but C ++ operators. The new operator can complete all the steps for creating an object (that is, the first step is to allocate memory for the object, and the second step is to call the constructor to initialize the memory). It also calls the constructor. Instance code:
MyObject * obj = new MyObject ();
The constructor can be overloaded. Based on the list of parameters passed by the user, you can determine which constructor to call to initialize the object.
The anti-operation operator of the new operator is delete. delete calls the Destructor first and then releases the memory. Instance code:
Delete obj;
Obj is an object pointer. obj can only release objects created by new, but cannot release objects created by malloc. In addition, after the delete release is used, the object pointer must be obj = NULL to prevent "wild pointer ".
The prompt is that the pointer variable is not initialized and its orientation is random. It is not NULL. If the if statement is used for judgment, it is considered as a valid pointer. In addition, after pointer variables are free or deleted, they only release the memory indicated by the pointer, but do not clear the pointer itself, the Pointer Points to the "junk" memory. If you use the if statement, it is also considered as a valid pointer. "Wild pointer" is very dangerous. A good programming habit is that in both cases, you need to set the pointer to NULL, which is the only way to avoid "wild pointer.
The code for allocating and releasing memory using new and delete is as follows:
# Include <iostream> using namespace std; class MyObject {public: MyObject () {cout <"call constructor." <endl ;}~ MyObject () {cout <"call destructor. "<endl;} void initialize () {cout <" call initialization. "<endl;} void destroy () {cout <" call destroy. "<endl ;}; int main () {MyObject * obj = new MyObject (); // apply for dynamic memory // TODOdelete obj; obj = NULL; return 0 ;}
Similarly, the MyObject class uses the new memory allocation and delete to release the memory. The program runs the constructor and destructor. The running result is as follows:
Call constructor.
Call destructor.
More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.c Ocoagame.net
For more exciting video courses, please follow the Cocos course in Zhijie class: http: // v.51w Ork6.com
Welcome to the Cocos2d-x Technology Discussion Group: 257760386 welcome to the knowledge of the iOS classroom public platform
Design game high score experts with cocos2d-x
Tongji? If so, contact me in the Southwest China 10, 178.
Cocos2dx development: how can we change the font when using CCLabelTTF? How can I find that the font cannot be changed? I mainly use it to output numbers.
Windows does not support very well. The old integrated graphics card does not support opengl1.5 or later versions. The font on the mobile phone is effective. You can try it on a black apple Virtual Machine by yourself.