代码改变世界

cocos2dx 2.2.5 hitWidget->onTouchEnded(pTouch, pEvent); 异常

2014-09-15 15:00  playerboy  阅读(466)  评论(0编辑  收藏  举报

源码:extensions\CocoStudio\GUI\System\UITouchGroup.cpp

void TouchGroup::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
ccArray* selectedWidgetArray = m_pSelectedWidgets->data;
int length = selectedWidgetArray->num;
for (int i=0; i<length; ++i)
{
Widget* hitWidget = (Widget*)(selectedWidgetArray->arr[0]);
m_pSelectedWidgets->removeObject(hitWidget);

hitWidget->onTouchEnded(pTouch, pEvent);   ---------这一行异常
}
}

异常发生后,查看变量信息,发现hitWidget有些信息已经被释放了,再看看hitWidget->onTouchEnded(pTouch, pEvent);这句之前是removeObject,想着有可能是removeObject中把hitWidget的部分内容给释放,所以,改为

void TouchGroup::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
ccArray* selectedWidgetArray = m_pSelectedWidgets->data;
int length = selectedWidgetArray->num;
for (int i=0; i<length; ++i)
{
Widget* hitWidget = (Widget*)(selectedWidgetArray->arr[0]);

hitWidget->onTouchEnded(pTouch, pEvent);

m_pSelectedWidgets->removeObject(hitWidget);

}
}

scroll中滚动再没出现过该异常。希望这个解决方案是正确的,如有问题,请指正。