Projects > cpp > Issues > Bug #752

We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.

Create Issue on Github

Enable and disable touch more times in touch handler call back function may cause memory leak

Bug #752 [Closed]
zhangxm 2011-10-17 23:22 . Updated about 10 years ago

Replace HelloWorld/Classes with pasted files, and run HelloWorld. menu2 will response the touch event even it is disabled.
The reason is, the add/remove operation is done in menu call back function, then touch delegate(menu2) will be added into
CCTouchDispatcher::m_pHandlersToAdd andCCTouchDispatcher:: m_pHandlersToRemove.

After the returning from menu call back function, CCTouchDispatcher::touches() will add/remove touch delegate contained in
CCTouchDispatcher::m_pHandlersToAdd andCCTouchDispatcher:: m_pHandlersToRemove into CCTouchDispatcher::m_pStandardHandlers
and CCTouchDispatcher::m_pTargetedHandlers.

Because the move operation is done before add, so the touch delegate will be added no matter if it is removed after adding.

The solution is:
change

void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches)
{
    pDelegate->m_eTouchDelegateType = ccTouchDelegateTargetedBit;

    CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pTargetedHandlers);
    }
    else
    {
        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}

into

void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches)
{
    pDelegate->m_eTouchDelegateType = ccTouchDelegateTargetedBit;

    CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
    if (! m_bLocked)
    {
        forceAddHandler(pHandler, m_pTargetedHandlers);
    }
    else
    {
                determine if pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and retrun.
        m_pHandlersToAdd->addObject(pHandler);
        m_bToAdd = true;
    }
}

change

void CCTouchDispatcher::removeDelegate(CCTouchDelegate *pDelegate)
{
    if (pDelegate == NULL)
    {
        return;
    }

    if (! m_bLocked)
    {
        forceRemoveDelegate(pDelegate);
    }
    else
    {
        ccCArrayAppendValue(m_pHandlersToRemove, pDelegate);
        m_bToRemove = true;
    }
}

into

void CCTouchDispatcher::removeDelegate(CCTouchDelegate *pDelegate)
{
    if (pDelegate == NULL)
    {
        return;
    }

    if (! m_bLocked)
    {
        forceRemoveDelegate(pDelegate);
    }
    else
    {
                determine if pHandler is contained in m_pHandlersToAdd, if so remove it from m_pHandlersToAdd and retrun.
        ccCArrayAppendValue(m_pHandlersToRemove, pDelegate);
        m_bToRemove = true;
    }
}

HelloWorldScene.cpp (2.6 kB) zhangxm, 2011-10-17 23:24

HelloWorldScene.h (805 Bytes) zhangxm, 2011-10-17 23:24

zhangxm 2011-10-17 23:22
  • Category set to 5
  • Target version set to 9
zhangxm 2011-10-17 23:24
zhangxm 2011-12-29 19:43
  • Assignee set to zhangxm
  • Target version changed from 9 to cocos2d-1.0.1-x-0.11.0
zhangxm 2011-12-30 14:34
  • Status changed from New to Resolved
  • % Done changed from 0 to 100

Applied in changeset commit:d1117f6341548b07bb4ce8ebe21a1e65729ef111.

zhangxm 2011-12-31 02:43
  • Status changed from Resolved to Closed
walzer@cocos2d-x.org 2012-05-25 08:02
  • Project changed from cocos2d-x to cpp
  • Category deleted (5)
  • Target version deleted (cocos2d-1.0.1-x-0.11.0)
wondeful18 2013-09-18 06:33

I think it is better to check the touch priority before do this optimize.

this is my modify code:

`void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{

CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
if (! m_bLocked)
{
forceAddHandler(pHandler, m_pStandardHandlers);
}
else
{

if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
{
CCTouchHandler *pOldHandler = findHandler(pDelegate);

        if (pOldHandler && pOldHandler->getPriority() == nPriority)
        {
            ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
            return;
        }
    }

    m_pHandlersToAdd->addObject(pHandler);
    m_bToAdd = true;
}

}
`

pyiap 2014-01-31 23:39

The optimization introduces a bug where the priority and Touch Mode will not get changed if the list is locked.
The most straight forward fix is to merge the pHandlersToAdd and pHandlersToRemove into a queue and process through all of them in the order they are received.

agenindonesia 2014-11-25 20:30

Pokerstar88.com Agen Texas Poker Dan Domino Online Indonesia Terpercaya
OK, I’ve updated the announcement and added a link to zipball on google code. Thank you for reminding me.
Agen Indonesia | Agen Bola | Poker Indonesia

ionliga 2014-11-28 14:34

The optimization introduces a bug where the priority and Touch Mode will not get changed if the list is locked.
The most straight forward fix is to merge the pHandlersToAdd and pHandlersToRemove into a queue and process through all of them in the order they are received

Pokerstar88.com Agen Texas Poker dan Domino Online Indonesia Terpercaya | Agen Bola | Taruhan Bola | Agen Bola | Olb365.com Agen Judi Bola Online, Agen Judi Casino Online Indonesia Terpercaya

Atom PDF

Status:Closed
Start date:2011-10-17
Priority:Normal
Due date:
Assignee:zhangxm
% Done:

100%

Category:-
Target version:-