cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.23k stars 7.05k forks source link

CCControlSwitch::create without labels will crash #9268

Open ghost opened 9 years ago

ghost commented 9 years ago

CCControlSwitch's internal methods assign a nullptr to Label* onLabel, Label* offLabel, which will crash when trying to add to the ClippingNode on line 196

clipper->addChild(onLabel);
clipper->addChild(offLabel);
ljluestc commented 1 year ago
ControlSwitch* ControlSwitch::create
(Label* label, Label* onLabel, Label* offLabel, Scale9Sprite* switchSprite, MenuItem* thumb, MenuItem* onThumb, MenuItem* offThumb)
{
    ControlSwitch *pRet = new ControlSwitch();
    pRet->initWithMaskSprite(switchSprite, thumb, onThumb, offThumb);
    pRet->autorelease();

    if (label)
    {
        pRet->addLabel(label);
        label->release();
        // Set the labels' text color based on the switch's state
        if (pRet->isOn())
        {
            label->setColor(pRet->getOnTitleColor());
        }
        else
        {
            label->setColor(pRet->getOffTitleColor());
        }
    }

    if (onLabel)
    {
        pRet->addLabel(onLabel);
        onLabel->release();
        onLabel->setVisible(pRet->isOn());
    }

    if (offLabel)
    {
        pRet->addLabel(offLabel);
        offLabel->release();
        offLabel->setVisible(!pRet->isOn());
    }

    return pRet;
}