Cocod2d-x -- about scenario switching, CCAction is not executed, still holding references causes memory leakage

Source: Internet
Author: User

The situation is as follows:

On the in-game combat page, the victory window is displayed. Click the next button in the window to switch to the resource loading scenario,

At this time, the game is paused, and all ccactions are paused. When switching a scenario, there are ccactions in the combat scenario.

If not, a reference of the CCAction object is still held. If the reference value is not 0 after the scenario is switched, memory leakage occurs.


Define the lifecycle of each Scene. Its parent class is as follows:

#ifndef SCREEN_H#define SCREEN_H#include "cocos2d.h"using namespace cocos2d;namespace giant {class Screen : public CCScene {protected:int m_width;int m_height;protected:Screen(int width, int height);public:virtual void show() = 0;virtual void close() = 0;virtual void pause() = 0;virtual void resume() = 0;};}#endif

Call show () to initialize resources when game scenarios enter

The game suspends pause () and suspends updates in the scenario, including CCAction

Call resume () to resume all previously paused updates.

Game scenario exit call close (), destroy view, release memory


Execute the following code when calling pause:

        m_pActionSet = CCDirector::sharedDirector()->getActionManager()->pauseAllRunningActions();        m_pActionSet->retain();

Run the following code when calling resume:

        CCDirector::sharedDirector()->getActionManager()->resumeTargets(m_pActionSet);        m_pActionSet->release();

Execute the following code when calling close:

        CCActionManager *pActionManager = CCDirector::sharedDirector()->getActionManager();        CCSetIterator iter;        for (iter = m_pActionSet->begin(); iter != m_pActionSet->end(); ++iter)        {            pActionManager->removeAllActionsFromTarget(*iter);        }        m_pActionSet->release();

You can release references in CCAction through the action in close ().

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.