setButtonEnabled(false) on UIPushButton clicked callback cause crash
Environment:Quick 3.3final, win7 64bit
----code----
local btnImages = {
normal = "images/ui/Button01.png",
pressed = "images/ui/Button01Pressed.png",
disabled = "images/ui/Button01Disabled.png",
}
local btnWidth = 100
local btnHeight = 60
local fontSize = 50
local text = "test"
local pushBtn
local callback = function ()
pushBtn:setButtonEnabled(false) --crash here
end
pushBtn = cc.ui.UIPushButton.new(btnImages, {scale9 = true})
:setButtonSize(btnWidth, btnHeight)
:setButtonLabel("normal", cc.ui.UILabel.new({
UILabelType = 2,
text = text,
size = fontSize
}))
:setButtonLabel("pressed", cc.ui.UILabel.new({
UILabelType = 2,
text = text,
size = fontSize,
color = cc.c3b(255, 64, 64)
}))
:setButtonLabel("disabled", cc.ui.UILabel.new({
UILabelType = 2,
text = text,
size = fontSize,
color = cc.c3b(0, 0, 0)
}))
:onButtonClicked(callback)
pushBtn:setPosition(display.cx, display.cy)
self:addChild(pushBtn)
@chenquanjun 我用你的代码,在3.3final上尝试,没有出现, 你的是必现吗?确定是在3.3final上?
我新建了一个,还是会崩溃 @htlxyz

0x0104F441 处的第一机会异常(在 Quick3d.exe 中): 0xC0000005: 读取位置 0xFEEEFEF2 时发生访问冲突。 0x0104F441 处有未经处理的异常(在 Quick3d.exe 中): 0xC0000005: 读取位置 0xFEEEFEF2 时发生访问冲突。

堆栈

看了一下代码,触发崩溃的条件是整个场景中只有一个触摸对象。
这里只有一个按钮,在点击回调里面设置按钮不可触摸,最后会调用destroyInstance方法。
void LuaTouchEventManager::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
for (auto it = touches.begin(); it != touches.end(); ++it)
{
m_touchingIds.erase(((Touch*)*it)->getID());
}
if (m_touchingIds.size())
{
dispatchingTouchEvent(touches, event, CCTOUCHREMOVED);
}
else
{
dispatchingTouchEvent(touches, event, CCTOUCHENDED); //执行完毕后Instance已经被析构
// remove all touching nodes
//CCLOG("TOUCH ENDED, REMOVE ALL TOUCH TARGETS");
_touchingTargets.clear(); //_touchingTargets已经被析构,再调用clear会出错
}
}
void LuaTouchEventManager::removeTouchableNode(LuaEventNode *node)
{
//...省略代码...
if (_touchableNodes.size() == 0 && m_touchDispatchingEnabled) //因为只有一个按钮而且按钮已经被禁用触摸
{
disableTouchDispatching();
destroyInstance(); //执行此代码
}
}
已修复。谢谢反馈!
貌似 这里还是会有问题
if (touchMode == LuaEventNode::modeTouchesAllAtOnce)
{
node->ccTouchesBegan(touches, event);
}
else
{
ret = node->ccTouchBegan(touchTarget->findTouch(touches), event);
}
if (ret)
{
_touchingTargets.pushBack(touchTarget);
//CCLOG("ADD TOUCH TARGET [%p]", touchTarget->getNode()->getNode());
}
在ccTouchBegan中执行了setEnabeld(false)了之后,就导致整个对象被delete了。那个_touchingTargets其实就已经是被释放了的,应该会出问题的