Cocos2d-x and objective-C Memory Management

Source: Internet
Author: User

Recently in the company to do a handheld frog game project, is using C ++ write cocos2d-x development, it is the iPhone cocos2d framework of another language version of the implementation, the biggest advantage is that it can run across platforms. You can run on Android and iPhone with simple modifications.


Because the functions and interfaces are similar to those of the objective-C version, the objective-C reference counting mechanism is also used for memory management. Carefully looked at the source code of the cocos2d-x, it is indeed well written, the code is organized very neatly. All their classes are inherited from ccobject,
Ccobject has methods such as retain (), release (), and autorelease (), which are consistent with the nsobject usage on objective-C. Whenever a ccobject object is initialized, its reference count reference-count is 1,
Call the retain () method reference-count plus 1, call the release () method reference-count minus 1, and release the object memory when reference-count is 0. The autorelease () method is used to manage objects in the ccautoreleasepool. After a drawing is completed, ccpoolmanager calls the release () method for each object in the current memory pool, some objects whose reference-count is 0 will be released by the memory.


Every time an object is obtained through the new and copy methods, it is obligated to call the release () method when it is not used. If we use other methods (such as static methods) do not call the release () method unless you call the retain () method to indicate that you want to own this object for a period of time. Customize the member variables in each class. If they are inherited from ccobject, you must retain () the new value before assigning a new value (),
Then, for the old value release (), the two steps cannot be omitted or transferred. When defining a method to return an object, if the object is created through new and copy in the method, you must call the autorelease () method before returning the object, for example, when a static method of many classes returns its instance, The autorelease () method has been called.

As long as the strict compliance with the above specifications, the problem of Memory leakage should be avoided, after writing C ++ programs, even if the use of cocos2d-x,
You can consider introducing

Memory Management by counting is simple and effective.

Related Article

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.