Skip to content

InputTextCalcTextSize assert #9069

Closed
Closed

Description

@sonoro1234

Version/Branch of Dear ImGui:

Version 1.92.4

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl.cpp

Compiler, OS:

windows 11 mingw-w64

Full config/build information:

No response

Details:

Assertion failed: text_end_display >= text_begin && text_end_display <= text_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 3950

I have no clue about how this happens. Any clue to debug it?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

Activity

  1. ocornut commented on Nov 15, 2025

    @ocornut
    Owner

    It is a multiline input ?
    Probably the same as #9007 already fixed.

    Victor you often open bug reports without providing information requested by guidelines (eg: a callstack) and keep making me guess. Please make a extra effort as provided relevant information. Thank you.

  2. sonoro1234 commented on Nov 17, 2025

    @sonoro1234
    Author

    providing information requested by guidelines (eg: a callstack)

    Sorry, I am having problems with debug: after the assert the debugger has no stack:

    Assertion failed: text_end_display >= text_begin && text_end_display <= text_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 3950
    [Thread 15720.0x1018 exited with code 3]
    [Thread 15720.0x272c exited with code 3]
    [Thread 15720.0x23e4 exited with code 3]
    [Thread 15720.0x2e7c exited with code 3]
    [Thread 15720.0x2770 exited with code 3]
    [Thread 15720.0x1a48 exited with code 3]
    [Thread 15720.0x457c exited with code 3]
    [Thread 15720.0x41c0 exited with code 3]
    [Thread 15720.0x8d0 exited with code 3]
    [Inferior 1 (process 15720) exited with code 03]
    (gdb) bt
    No stack.
    (gdb)
    

    Still investigating

  3. ocornut commented on Nov 17, 2025

    @ocornut
    Owner

    Maybe simply update and see if the fix above is the same as your issue.

  4. sonoro1234 commented on Nov 17, 2025

    @sonoro1234
    Author

    No luck

    Assertion failed: text_end_display >= text_begin && text_end_display <= text_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 3958
    [Thread 9164.0x3a2c exited with code 3]
    [Thread 9164.0x255c exited with code 3]
    [Thread 9164.0x43e0 exited with code 3]
    [Thread 9164.0xc64 exited with code 3]
    [Thread 9164.0x1d34 exited with code 3]
    [Thread 9164.0x1b90 exited with code 3]
    [Thread 9164.0x4e7c exited with code 3]
    [Thread 9164.0x339c exited with code 3]
    [Thread 9164.0x3ba8 exited with code 3]
    [Inferior 1 (process 9164) exited with code 03]
    (gdb) bt
    No stack.
    (gdb)
    
  5. ocornut commented on Nov 17, 2025

    @ocornut
    Owner

    Can't help you without more information.

  6. ocornut commented on Nov 17, 2025

    @ocornut
    Owner

    Try with this and report all log output for the last two frames prior to the assert triggering:

    imgui-4050cf9-debug for 9069.patch

  7. sonoro1234 commented on Nov 17, 2025

    @sonoro1234
    Author

    patch still not used.
    Compiled and debuged on MSVC 2022.
    This is the call stack:

    >	cimgui_glfw.dll!InputTextCalcTextSize(ImGuiContext * ctx, const char * text_begin, const char * text_end_display, const char * text_end, const char * * out_remaining, ImVec2 * out_offset, int flags) Línea 3958	C++
     	cimgui_glfw.dll!InputTextLineIndexGetPosOffset(ImGuiContext & g, ImGuiInputTextState * state, ImGuiTextIndex * line_index, const char * buf, const char * buf_end, int cursor_n) Línea 4627	C++
     	cimgui_glfw.dll!ImGui::InputTextEx(const char * label, const char * hint, char * buf, int buf_size, const ImVec2 & size_arg, int flags, int(*)(ImGuiInputTextCallbackData *) callback, void * callback_user_data) Línea 5441	C++
     	cimgui_glfw.dll!ImGui::InputText(const char * label, char * buf, unsigned __int64 buf_size, int flags, int(*)(ImGuiInputTextCallbackData *) callback, void * user_data) Línea 3941	C++
     	cimgui_glfw.dll!igInputText(const char * label, char * buf, unsigned __int64 buf_size, int flags, int(*)(ImGuiInputTextCallbackData *) callback, void * user_data) Línea 953	C++
    
    

    text_end_display is one more than text_end

    Image
  8. ocornut commented on Nov 17, 2025

    @ocornut
    Owner

    Thanks! That narrows it down already. Ideally try to come up with a repro (e.g. InputText value and actions performed)
    Should aim to fix this before next release.

  9. sonoro1234 commented on Nov 17, 2025

    @sonoro1234
    Author

    Ideally try to come up with a repro (e.g. InputText value and actions performed)

    It happens in a quite complex custom widget and happens sparsely.

    I am applying the patch to report

  10. sonoro1234 commented on Nov 17, 2025

    @sonoro1234
    Author

    Thats it:

    [03831] Flags=0x00000200, line_start=+0, cursor=+5, buf_end=+5
    [03832] Flags=0x00000200, line_start=+0, cursor=+5, buf_end=+5
    [03833] Flags=0x00000200, line_start_idx=0, obj->TextLen=5
    [03833] Flags=0x00000200, line_start_idx=0, obj->TextLen=5
    [03833] Flags=0x00000200, line_start=+0, cursor=+5, buf_end=+5
    [03834] Flags=0x00000200, line_start_idx=0, obj->TextLen=6
    [03834] Flags=0x00000200, line_start_idx=0, obj->TextLen=6
    [03834] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+6
    [03835] Flags=0x00000200, line_start_idx=0, obj->TextLen=6
    [03835] Flags=0x00000200, line_start_idx=0, obj->TextLen=6
    [03835] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+6
    [03836] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+5
    Assertion failed: cursor_ptr <= buf_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 4633
    
  11. ocornut commented on Nov 17, 2025

    @ocornut
    Owner

    What does the custom code looks like ?

  12. sonoro1234 commented on Nov 18, 2025

    @sonoro1234
    Author

    This is the custom control, translated from @pixtur C# #3494

    function W.SingleValueEdit()
            local JogDialStates = 
            {
                Inactive=0,
                Dialing=1,
                StartedTextInput=2,
                TextInput=3,
            }
            
            local function IsNaN(v) return v~=v end     
            --local InputEditStateFlags = {ResetToDefault=0,Finished=1,Started=2,Modified=3,Nothing=4}
            local InputEditStateFlags = {ResetToDefault=false,Finished=false,Started=false,Modified=true,Nothing=false}
    
            local SVE = {_state = JogDialStates.Inactive, _jogDialText = "", _numberFormat = "%.3f"}
            
            function SVE:FormatValueForButton(value)
                return string.format(self._numberFormat, value);
            end
    
            function SVE:SetState(newState)
                if newState == JogDialStates.Inactive then
                        self._activeJogDialId = 0;
                elseif newState == JogDialStates.Dialing then
                        self._center = ig.GetMousePos();
                -- elseif newState == JogDialStates.StartedTextInput then
                        --break;
                -- elseif newState == JogDialStates.TextInput then
                        --break;
                end
                self._state = newState;
            end
            
            --- A horrible ImGui work around to have button that stays active while its label changes.  
            function SVE:DrawButtonWithDynamicLabel(label, size)
                --local color1 = ig.GetColorU32(ig.lib.ImGuiCol_Text);
                --local keepPos = ig.GetCursorScreenPos();
                --ig.Button("##dial"..tostring(self), size);
                --ig.GetWindowDrawList():AddText(keepPos + ig.ImVec2(4, 4), color1, label);
                ig.SetNextItemWidth(size.x)
                local ttt = ffi.new("char[20]",label)
                ig.InputText("##dial"..tostring(self),ttt,20,ig.lib.ImGuiInputTextFlags_ReadOnly)
            end
            
            function SVE:DrawInt(value, min, max, scale, size)
                local doubleValue = ffi.new("int[1]",value[0])
                local result = self:Draw(doubleValue, min, max, scale, "%.0f", size);
                value[0] = doubleValue[0];
                return result;
            end
            function SVE:Draw(value, min, max, scale, format, size)
                size = size or ig.CalcTextSize("xxx.xxx") + ig.GetStyle().FramePadding*2
                min = min or (- ig.FLT_MAX)
                max = max or (ig.FLT_MAX)
                scale = scale or 1
                format = format or "%.3f" 
                self._numberFormat = format;
                
                local iog = ig.GetIO();
                local id = ig.GetID("jog"..tostring(self));
                if (id == self._activeJogDialId) then
    
                    if self._state == JogDialStates.Dialing then
                            ig.PushStyleColor(ig.lib.ImGuiCol_Button, ig.U32(0,0,0,1));
                            ig.PushStyleColor(ig.lib.ImGuiCol_ButtonHovered, ig.U32(0,0,0,1));
                            ig.PushStyleColor(ig.lib.ImGuiCol_ButtonActive, ig.U32(0,0,0,1));
                            self:DrawButtonWithDynamicLabel(self:FormatValueForButton(self._editValue), size);
                            ig.PopStyleColor(3);
    
                            if (ig.IsMouseReleased(0)) then
                                local wasClick = ig.GetIO().MouseDragMaxDistanceSqr[0] < 4;
                                if (wasClick) then
                                    if (iog.KeyCtrl) then
                                        self:SetState(JogDialStates.Inactive);
                                        return InputEditStateFlags.ResetToDefault;
                                    else
                                        self:SetState(JogDialStates.StartedTextInput);
                                    end
                                else
                                    self:SetState(JogDialStates.Inactive);
                                end
                            elseif (ig.IsItemDeactivated()) then
                                self:SetState(JogDialStates.Inactive);
                            else
                                self:JogDialOverlayDraw(iog, min, max, scale);
                            end
    
                    elseif self._state == JogDialStates.TextInput or self._state ==  JogDialStates.StartedTextInput then
                            if self._state ==  JogDialStates.StartedTextInput then
                                ig.SetKeyboardFocusHere();
                                self:SetState(JogDialStates.TextInput);
                            end
                            ig.PushStyleColor(ig.lib.ImGuiCol_Text, IsNaN(self._editValue)
                                                                    and ig.U32(1,0,0,1)
                                                                    or ig.U32(1,1,1,1));
                            ig.SetNextItemWidth(size.x);
                            local ttt = ffi.new("char[20]",self._jogDialText or "")
                            local tinput = false
                            if ig.InputText("##dialInput"..tostring(self), ttt, 20) then
                                self._jogDialText = ffi.string(ttt)
                                tinput = true
                            end
                            ig.PopStyleColor();
    
                            if (ig.IsItemDeactivated()) then
                                self:SetState(JogDialStates.Inactive);
                                if (IsNaN(self._editValue)) then
                                    self._editValue = self._startValue;
                                end
                            end
    
                            self._editValue = tonumber(self._jogDialText) or 0;
                            if tinput then
                                self._startValue = self._editValue
                                value[0] = self._editValue;
                                return true
                            end
                    end
    
                    value[0] = self._editValue;
                    if (self._state == JogDialStates.Inactive) then
                        return InputEditStateFlags.Finished;
                    end
    
                    self._editValue = math.floor((self._editValue * 1000) + 0.5) / 1000;
                    return math.abs(self._editValue - self._startValue) > 0.0001 and InputEditStateFlags.Modified or InputEditStateFlags.Started;
                end
    
                self:DrawButtonWithDynamicLabel(self:FormatValueForButton(value[0]), size);
                --if (ig.IsItemActivated()) then
                 if (ig.IsItemClicked()) then
                    self._activeJogDialId = id;
                    self._editValue = value[0];
                    self._startValue = value[0];
                    self._jogDialText = self:FormatValueForButton(value[0]);
                    self:SetState(JogDialStates.Dialing);
                end
    
                return InputEditStateFlags.Nothing;
            end
    
            -- Draws a circular dial to manipulate values with various speeds
            function SVE:JogDialOverlayDraw(iog, min, max, scale)
                local SegmentWidth = 90;
                local NeutralRadius = 10;
                local RadialIndicatorSpeed = (2 * math.pi / 20);
                local Padding = 2;
    
                local SegmentSpeeds = {
                                       (0.15 / math.pi),
                                       (10 * 0.5 / math.pi)}
    
                local SegmentColor = ig.U32(1, 1, 1, 0.2);
                local ActiveSegmentColor = ig.U32(1, 1, 1, 0.2);
     
                    local foreground = ig.GetForegroundDrawList();
                    ig.SetMouseCursor(ig.lib.ImGuiMouseCursor_Hand);
    
                    local pLast = iog.MousePos - iog.MouseDelta - self._center;
                    local pNow = iog.MousePos - self._center;
    
                    local distanceToCenter = math.sqrt(pNow.x*pNow.x+pNow.y*pNow.y) --pNow.Length();
    
                    local r = NeutralRadius;
                    local activeSpeed = 0;
    
                    local rot = math.fmod((self._editValue - self._startValue) * RadialIndicatorSpeed, 2 * math.pi);
    
                    for index, segmentSpeed in ipairs(SegmentSpeeds) do
                        local isLastSegment = (index == #SegmentSpeeds)
                        local isActive =
                            (distanceToCenter > r and distanceToCenter < r + SegmentWidth) or
                            (isLastSegment and distanceToCenter > r + SegmentWidth);
    
                        if (isActive) then
                            activeSpeed = segmentSpeed;
                            local opening = 3.14 * 1.75;
    
                            foreground:PathArcTo(
                                                 self._center,
                                                 r + SegmentWidth / 2,
                                                 rot,
                                                 rot + opening,
                                                  64);
                            foreground:PathStroke(ActiveSegmentColor, false, SegmentWidth - Padding);
                        else
                        
                            foreground:AddCircle(self._center,
                                                  r + SegmentWidth / 2,
                                                 SegmentColor,
                                                  64,
                                                  SegmentWidth - Padding);
                        end
    
                        r = r + SegmentWidth;
                        index = index + 1
                    end
    
                    local aLast = math.atan2(pLast.x, pLast.y);
                    local aNow = math.atan2(pNow.x, pNow.y);
                    local delta = aLast - aNow;
                    if (delta > 1.5) then
                        delta = delta - (2 * math.pi);
                    elseif (delta < -1.5) then
                        delta = delta + (2 * math.pi);
                    end
    
                    --delta = math.floor((delta * 50)+0.5) / 50;
                    if iog.KeyCtrl then activeSpeed = activeSpeed*0.1 end
                    self._editValue = self._editValue + delta * activeSpeed * scale * 100;
                    self._editValue = math.min(max, math.max(min,self._editValue))
                end
           
            
        return SVE
    end
    

    It is used as:

    local igwin = require"imgui.window"
    
    local win = igwin:GLFW(800,400, "compute graph",{vsync=false})
    ig = win.ig
    ffi = require"ffi"
    local igw = require"anima.igwidgets"
    
    local value = ffi.new("float[1]",34)
    local SVE = igw.SingleValueEdit()
    function win:draw(ig)
    	SVE:Draw(value,0.1,10)
    end
    
    win:start()

    I tried to make a simpler reproducer with

    local igwin = require"imgui.window"
    
    local win = igwin:GLFW(800,400, "compute graph",{vsync=false})
    ig = win.ig
    ffi = require"ffi"
    
    function FormatValueForButton(value)
           return string.format("%.3f", value);
    end
    function DrawButtonWithDynamicLabel(label, size)
    	ig.SetNextItemWidth(size.x)
    	local ttt = ffi.new("char[20]",label)
    	ig.InputText("##dial",ttt,20,ig.lib.ImGuiInputTextFlags_ReadOnly)
    end
    local value = ffi.new("float[1]",34)
    
    function win:draw(ig)
    	value[0] = math.random()*100
    	ig.Begin("aaaa")
    	local size = ig.CalcTextSize("xxx.xxx") + ig.GetStyle().FramePadding*2
    	DrawButtonWithDynamicLabel(FormatValueForButton(value[0]),size)
    	ig.End()
    end
    
    win:start()

    But It gives another assert when mouse clicking in inputText:

    [00165] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+6
    [00166] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+6
    [00167] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+6
    [00168] Flags=0x00000200, line_start=+0, cursor=+6, buf_end=+5
    Assertion failed: cursor_ptr <= buf_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 4633
    
  13. sonoro1234 commented on Nov 18, 2025

    @sonoro1234
    Author

    With first code:

    Image

  14. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    But It gives another assert when mouse clicking in inputText:

    Well that seems like exactly the same assert, why do you say it is a "another assert"? ?

    I can now repro something by swapping text contents in the _ReadOnly field to make it shorter than current selection:

    static char buf[20] = "12345";
    if (ImGui::IsKeyPressed(ImGuiKey_F1))
        strcpy(buf, "xxx");
    ImGui::InputText("test", buf, 20, ImGuiInputTextFlags_ReadOnly);

    Select text then press F1.

    I'll work from this.

  15. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    Pushed a fix 9afc62d (please confirm).

  16. sonoro1234 commented on Nov 18, 2025

    @sonoro1234
    Author

    Well that seems like exactly the same assert, why do you say it is a "another assert"? ?

    Assertion failed: text_end_display >= text_begin && text_end_display <= text_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 3958
    

    vs

    Assertion failed: cursor_ptr <= buf_end, file C:\LuaGL\gitsources\anima\LuaJIT-ImGui\cimgui\imgui\imgui_widgets.cpp, line 4633
    

    Pushed a fix 9afc62d (please confirm).

    I will check

  17. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    The later assert is the one added by the patch in an earlier spot, and is the same as you reported in #9069 (comment)

  18. sonoro1234 commented on Nov 18, 2025

    @sonoro1234
    Author

    I can now repro something by swapping text contents in the _ReadOnly field to make it shorter than current selection:

    static char buf[20] = "12345";
    if (ImGui::IsKeyPressed(ImGuiKey_F1))
    strcpy(buf, "xxx");
    ImGui::InputText("test", buf, 20, ImGuiInputTextFlags_ReadOnly);

    Select text then press F1.

    I cant repro with this because when text is selected F1 is not detected

    Pushed a fix 9afc62d (please confirm).

    My simple reproducer does not assert with the fix

  19. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    I cant repro with this because when text is selected F1 is not detected

    Why wouldn't it? It's using ImGui::IsKeyPressed() which doesn't do any ownership check.

    I'll assume this is generally fixed, if it's not please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址