nicbarker / clay

High performance UI layout library in C.
https://nicbarker.com/clay
zlib License
1.29k stars 31 forks source link

[Bug] Memory bug caused by overflow in text measurement cache #43

Closed SushilRagoonath closed 1 month ago

SushilRagoonath commented 1 month ago

Hi there, I'm enjoying creating something with this library using the odin bindings. I have an application where I keep changing the text element and after around 40 changes(its inconsistent) the application freezes up and gets stuck in line 2471 of after calling clay.EndLayout() I have the state of the local variables here as I recompiled the library with debug symbols. I cannot seem to get any warnings or better errors so let me know the best way to do that if it would help. I am on windows but compiling the library on ubuntu. Here is the state image Theres nothing particularly interesting about my layout except the text changes on a button press.

createLayout :: proc(lerpValue: f32) -> clay.ClayArray(clay.RenderCommand) {
    clay.BeginLayout()
    if clay.Container(
        clay.ID("LandingPage1Desktop"),
        clay.Layout({sizing = {width = clay.SizingGrow({}), height = clay.SizingFit({min = cast(f32)windowHeight - 70})}, childAlignment = {y = .CENTER}, padding = {x = 50}}),
    ) {
        rad:: 50.0
        // if mainText == nil {
        //     log.error("wtf")
        //     mainText = generate_text(200)
        // }
        if clay.Rectangle(
            clay.ID("yo"),
            clay.Layout(
                clay.LayoutConfig{
                    childAlignment = {.CENTER,.CENTER},
                    childGap = 15,
                    layoutDirection = .TOP_TO_BOTTOM,
                    sizing = clay.Sizing{clay.SizingGrow({}),clay.SizingGrow({}) },
                    padding = {16,16}
                }
            ),
            clay.RectangleConfig(clay.RectangleElementConfig{
                color = {104,150,205,255},
                cornerRadius = {rad,rad,rad,rad}
            })
        ){
            clay.Text(
                clay.ID("the-main-text"),
                mainText,
                &mainTextConfig)
        }
    }
EDIT: I should mention the text is random so thats probably why its inconsistent, Also its with line wrap, here is the config
mainTextConfig := clay.TextElementConfig {
    fontId    = FONT_ID_BODY_24,
    fontSize  = 24,
    textColor = clay.Color{255,255,255,255},
    wrapMode = .Words,
}
nicbarker commented 1 month ago

@SushilRagoonath Hello, sorry you're having trouble. I have a suspicion about what this could be, will investigate today and let you know what I find!

nicbarker commented 1 month ago

I've merged a change that I think should fix this issue in https://github.com/nicbarker/clay/pull/44, please try out main and let me know if it works for you 🙂

SushilRagoonath commented 1 month ago

Cool, it works now, ran it for a few minutes and it worked fine. Thanks