Crash solution due to high memory during cocos2d-x scenario Switching

Source: Internet
Author: User

Recently in a cocos2d-x project, encountered a problem, is running on the PC is OK, but in the iPad and andriod above, in the scenario switching often fails, when detected using Apple's instruments tool, it was found that the memory remained at around 30 or 40 MB while the scene was running normally. However, during scenario switching, the memory would reach 70 or 80 Mb in an instant, in some scenarios where there are many materials or more layers, it will reach more than one hundred MB. As you know, the maximum memory size of ipad1 is 128 MB. If this program runs on ipad1. If you encounter problems, you can analyze them step by step and find them step by step.

(1) first check the retain items in all scenarios to see if there is any release during onexit. After this check, it will still crash, so this is not the root cause.

(2) For scenarios with several layers, in init, the first layer is loaded first, instead of loading all the layers, load the corresponding layer when switching the layer. This method is really effective. When switching to this scenario, the memory is reduced by about half. However, for scenarios with only one layer, the switchover fails, so the problem is still incomplete.

(3) We know that there will be an init function, an onentertransitiondidfinish function, an onexit function, and init to implement initialization. onentertransitiondidfinish is executed after init, onexit recycles the resources allocated during init when the scenario exits. This is an interesting phenomenon found during debugging, that is, when switching from scenario 1 to scenario 2, the memory will be very high at the moment of switching, but after a while, the memory will return to a stable State. For example, when switching, the memory will reach 80 Mb, and after switching, the memory will drop to 50 MB. Cause Analysis: it is suspected that the memory in the previous scenario has not been released, and the memory in this scenario has been allocated. Therefore, the two are stacked together, which is relatively high. So I add a breakpoint to the onexit function of the first scenario, add a breakpoint to the init and onentertransitiondidfinish functions of the second scenario, and then run the program, the program first goes to init In the second scenario, then returns to onexit In the first scenario, and finally to onentertransitiondidfinish In the second scenario. I suddenly realized that when switching the scenario, the onexit function of the first scenario was not executed immediately, but the resource was first loaded in the init of the second scenario, then return to the first scenario to release the resource, and finally to onentertransitiondidfinish.

Final Solution:

Put the initialization of some resources in onentertransitiondidfinish for initialization. Should those resources be put in onentertransitiondidfinish for initialization, and those resources can only be put in init?

(1) A background image can only be placed in init. For example, some genie to be seen during scenario switching must be placed in init. Otherwise, the background or some genie will not be seen during scenario switching.

(2) Some animations and actions of the image genie can be initialized in onentertransitiondidfinish.

For example:

For example, if a ship is moving, the ship's genie will be displayed during scenario switching, so it must be placed in init.

// Boat genie Loading

m_boatAction = CCSprite::spriteWithFile(s1_little_boat1);addChild(m_boatAction);m_boatAction->setPosition( CCPointMake(s.width/2, s.height/2+130));m_boatAction->setScale(0.3);m_boatAction->retain();


However, you can put the boat movement action in onentertransitiondidfinish to initialize and execute it.

CCSize s = CCDirector::sharedDirector()->getWinSize();CCAnimation* animation = CCAnimation::animation();char frameName[100] = {0};for( int i=1;i<=5;i++){sprintf(frameName, "scene1/little_boat%d.png", i);animation->addFrameWithFileName(frameName);}CCActionInterval*  action = CCAnimate::actionWithDuration(2, animation, false);repeatAction = CCRepeatForever::actionWithAction(action);repeatAction->retain();
In these three steps, we can avoid crash caused by memory overuse During scenario switching.


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: 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.