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

CalcTextSize can produce a segfault #4769

Closed
bit69tream opened this issue Nov 27, 2021 · 5 comments
Closed

CalcTextSize can produce a segfault #4769

bit69tream opened this issue Nov 27, 2021 · 5 comments

Comments

@bit69tream
Copy link

bit69tream commented Nov 27, 2021

Dear ImGui 1.86 WIP (18511)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201703
define: __linux__
define: __GNUC__=11
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000000
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1920.00,1052.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Version/Branch of Dear ImGui:

Version: 1.85
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Compiler: g++ 11.2.0
Operating System: gentoo

My Issue/Question:

Calling CalcTextSize like ImGui::CalcTextSize(""); produces segfault, but calling it like ImGui::CalcTextSize("", nullptr, true); wont segfault.
String content doesn't affect presence of the error.

There is also what gdb says about that:

Thread 1 "finally" received signal SIGSEGV, Segmentation fault.
0x00005555555b9232 in ImFont::CalcTextSizeA (this=0x0, size=0, max_width=3.40282347e+38, wrap_width=-1, text_begin=0x55555562807d "", text_end=0x55555562807d "", remaining=0x0) at imgui/imgui_draw.cpp:3432
3432	    const float scale = size / FontSize;
@bit69tream
Copy link
Author

Would like to add, that I'm calling CalcTextSize function after ImGui is initialized, just before the event loop.

Also, turned out that it doesn't segfault for me only when hide_text_after_double_hash is true and provided string is empty. Sorry for not including that in the original issue message.

But there is a gdb trace of segfault caused by ImGui::CalcTextSize("asdf", nullptr, true);

Thread 1 "finally" received signal SIGSEGV, Segmentation fault.
0x00005555555b9232 in ImFont::CalcTextSizeA (this=0x0, size=0, max_width=3.40282347e+38, wrap_width=-1, text_begin=0x55555562807d "asdf", text_end=0x555555628081 "", remaining=0x0) at imgui/imgui_draw.cpp:3432
3432	    const float scale = size / FontSize;

@bit69tream bit69tream changed the title CalcTextSize can produce a segfault when hide_text_after_double_hash parameter is set to false CalcTextSize can produce a segfault Nov 27, 2021
@thedmd
Copy link
Contributor

thedmd commented Nov 28, 2021

Please check if font atlas is generated. If you do not do that by yourself atlas if generation is triggered by the backend while creating font atlas texture via ImFontAtlas::GetTexDataAsXXX call. This is probably after your call to ImGui::CalcTextSize(). Please call ImFontAtlas::Build() before calling text related functions before first frame.

@bit69tream
Copy link
Author

Oh, thanks. But I cannot figure out how exactly I need to call ImFontAtlas::Build().
Just doing ImGui::GetIO().Fonts->Build(); doesn't seem to help.

@ocornut
Copy link
Owner

ocornut commented Nov 28, 2021 via email

@bit69tream
Copy link
Author

bit69tream commented Nov 28, 2021

Yeah, if I put everything after a NewFrame call it works fine. Just don't want to recalculate the dimensions on every frame.
I guess I can just make a boolean indicator if it was already calculated or not. ¯\(ツ)

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

No branches or pull requests

3 participants