Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label create with long string cause random crash in windows. #2256

Closed
Sevael opened this issue Dec 5, 2024 · 1 comment
Closed

Label create with long string cause random crash in windows. #2256

Sevael opened this issue Dec 5, 2024 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@Sevael
Copy link

Sevael commented Dec 5, 2024

Create a long text with label, cause random crash in windows,
the reason is the size over the limit of texture max size.

Sample code:

    std::string str;
    for (int i = 0; i < 30; i++) {
        str+="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    }
    Label* label = Label::create();
    label->setFontSize(50);
    label->setAnchorPoint(Vec2::ZERO);
    label->setTextColor(Color4B::WHITE);
    label->setPosition(0, 100);
    label->enableWrap(false);
    label->setString(str);
    addChild(label);

it is better to do some protection in "Device::getTextureDataForText" inside the file:
platform/win32/Device-win32.cpp
platform/android/Device-android.cpp
platform/ios/Device-ios.mm
platform/mac/Device-mac.mm
platform/linux/Device-linux.cpp

protected sample:

Data Device::getTextureDataForText() {

    SIZE size = {(LONG)textDefinition._dimensions.width, (LONG)textDefinition._dimensions.height};
    AX_BREAK_IF(!dc.drawText(text, size, align, textDefinition._fontName.c_str(), (int)textDefinition._fontSize,
                             textDefinition._enableWrap, textDefinition._overflow));

    auto driver = backend::DriverBase::getInstance();  //<<======== here
    int maxSize = driver->getMaxTextureSize();
    if (size.cx > maxSize || size.cy > maxSize) {
        width = 0;
        height = 0;
        return ret;
    }

}

othe platform is same as so on

i try to let it show normal if oversize, but i think it is bad idea. so i recommend just return false if failed.

    auto driver = backend::DriverBase::getInstance();
    int maxSize = driver->getMaxTextureSize();
    std::string textCut = text.data();
    while (size.cx > maxSize || size.cy > maxSize) {  //test only code, don't used
        float fCut = (float)maxSize / (float)size.cx; //bad
        textCut = textCut.substr(0, (size_t)(text.length() * fCut)).data(); //bad
        size = { (LONG)textDefinition._dimensions.width, (LONG)textDefinition._dimensions.height };
        AX_BREAK_IF(!dc.drawText(textCut, size, align, textDefinition._fontName.c_str(), (int)textDefinition._fontSize,
                                 textDefinition._enableWrap, textDefinition._overflow));
    }
    text = textCut;
@halx99 halx99 closed this as completed in 775b752 Dec 5, 2024
@halx99 halx99 added this to the 2.3.0 milestone Dec 5, 2024
@halx99 halx99 added the enhancement New feature or request label Dec 5, 2024
@halx99
Copy link
Collaborator

halx99 commented Dec 5, 2024

fixed, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants