[IOS] the process of solving the problem of memory release in cocos2d-x

Source: Internet
Author: User

At night to help students to see a Cocos2d-x project memory release problem, Win32 version running no problem, occupied memory only 20 mb, but on the iOS platform, crash is removed without switching between scenarios.

Several problems found during compilation are recorded here:

(1) In Win32, if Init () in cclayer forgets to add the return value "Return true", there is no problem, but in iOS, although the same code will be compiled in xcode, ccnode cannot be used to addchild during running, so please pay attention to the bad habits of children's shoes ~~

(2) cocos2d-x in version 1.0.1-x-0.12.0 touches-> locationinview () requires parameters, while1.0.1-x-0.13.0
This parameter is not required in Beta. So the two platform cocos2d-x version must be unified.

 

Well, the compilation is successful. Let's take a look at how to solve the problem of memory release.

First, we will introduce a tool: xcode comes with instruments, which allows you to see the real-time memory and CPU information occupied by the application (of course, it has more than these functions ~~~). For more information, see the following article.

First View

(1) connect to the real machine and compile the application to the real machine.

(2) start the application from instruments

(3) When the program is started, it only occupies about 8 MB of memory, and then click to switch the scenario.

(4) In switching scenarios, the memory usage is 30 mb. After switching, the memory usage is still 30 mb.

(5) Continue switching, the memory usage continues to increase, and all scenarios are not completely switched. The program crash

 

Preliminary analysis: After switching the scenario, the memory is not released. Check the Code:

(1) Check the object that has called the retain () method and see if there are any objects that have not called release () in onexit () [All have been added with release, not this problem]

(2) manually call ccdirector: sharedirector-> purgedatacache () in the onexit () method to clear the cache. [memory usage is reduced slightly, but it is by no means the root cause of the problem]

(3) Check the cclayer creation method and confirm that the cclayer: node (); method is automatically called when autorelease () is called.Autorelease (), if yes
If new cclayer is used, you need to manually call autorelease (). [okay, this is not the problem]

(4) No way, continue to check the onexit () function [right, it is it, it is it .....]

(5) finally found the cause of the problem. At onexit (), the onexit () of the parent class was not called (),Cclayer: onexit (); if only the release of the retain object is added, the memory of the current layer will not be released. [Fix
It !!!!!!]

(6) The command + r memory is always 20 mb. [Let's leave without going ...]

 

Test the Android version tomorrow .. It is estimated that there are more problems ·····


During the test today, another problem was found. If ccmove was used on iPad 2 to move ccsprite, a very serious pop-up screen would occur and some information was found, someone has already reported this on the official website .. Http://www.cocos2d-x.org/boards/6/topics/6802

The solution is also available.

You only need to add

CCDirector::sharedDirector()->setDepthTest(false);


Or

CCDirector::sharedDirector()->setProjection(kCCDirectorProjection2D);

 

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.