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

Using PopFont() inside a window causes crash when rendering text in that window #3875

Closed
codingoat opened this issue Mar 4, 2021 · 6 comments

Comments

@codingoat
Copy link

Version: 1.81
Branch: master
Back-ends: imgui_impl_sdl.cpp + imgui_impl_opengl3.cpp
Operating System: Windows 10

My Issue/Question:

Hi! I am trying to set a custom font for my window titles (following the sample in this issue), and ran into a crash in the process.

I have loaded two fonts during initialization, fontSans and fontTitle. As expected, fontSans acts as the default font. However, if I try to set a window title font to fontTitle

ImGui::PushFont(fontTitle);
ImGui::Begin("Window");
ImGui::PopFont();
ImGui::Text("Hello!");
ImGui::End();

the program crashes with the following message: Assertion failed: font->ContainerAtlas->TexID == _CmdHeader.TextureId, file ...\imgui\imgui_draw.cpp, line 1402

Call stack:

 	ucrtbased.dll!00007ffee4687c75()	Unknown
 	ucrtbased.dll!00007ffee4687e13()	Unknown
 	ucrtbased.dll!00007ffee469e01d()	Unknown
 	ucrtbased.dll!00007ffee46a3c65()	Unknown
 	ucrtbased.dll!00007ffee46a37d7()	Unknown
 	ucrtbased.dll!00007ffee46a1868()	Unknown
 	ucrtbased.dll!00007ffee46a41cf()	Unknown
>	SpirMark.exe!ImDrawList::AddText(const ImFont * font, float font_size, const ImVec2 & pos, unsigned int col, const char * text_begin, const char * text_end, float wrap_width, const ImVec4 * cpu_fine_clip_rect) Line 1402	C++
 	SpirMark.exe!ImGui::RenderTextWrapped(ImVec2 pos, const char * text, const char * text_end, float wrap_width) Line 2663	C++
 	SpirMark.exe!ImGui::TextEx(const char * text, const char * text_end, int flags) Line 249	C++
 	SpirMark.exe!ImGui::TextV(const char * fmt, char * args) Line 273	C++
 	SpirMark.exe!ImGui::Text(const char * fmt, ...) Line 261	C++
 	SpirMark.exe!CMyApp::RenderUI() Line 347	C++
 	SpirMark.exe!CMyApp::Render() Line 312	C++
 	SpirMark.exe!SDL_main(int argc, char * * args) Line 212	C++
 	SpirMark.exe!main_utf8(int argc, char * * argv) Line 127	C
 	SpirMark.exe!main_getcmdline(...) Line 159	C
 	SpirMark.exe!main(int argc, char * * argv) Line 172	C

Meanwhile, doing this

ImGui::PushFont(fontTitle);
ImGui::Begin("Window");
ImGui::Text("Hello!");
ImGui::PopFont();
ImGui::End();

ImGui::Begin("Window 2");
ImGui::Text("Hello default!");
ImGui::End();

works, with the second window using the default font (as expected).

What am I doing wrong?

@ocornut
Copy link
Owner

ocornut commented Mar 4, 2021

Thank you for the report. I can confirm there is a bug with PopFont() leading to an empty stack.

@ocornut
Copy link
Owner

ocornut commented Mar 4, 2021

PushFont/PopFont are described as being "shared stack" but the way they behave actually isn't correctly behaving cross window.
So one way to see it is that it is a documentation bug (and PushFont/PopFont is not a shared stack)
But ideally I think everyone wants this to be a shared stack so I think it should be fixed toward honoring that behavior.

@ocornut
Copy link
Owner

ocornut commented Mar 4, 2021

Same as #3224 moving there.

@ocornut ocornut closed this as completed Mar 4, 2021
@ocornut
Copy link
Owner

ocornut commented Mar 4, 2021

I don't mind to cop out from this but last time I looked at this - and been looking for two hours now - the right fix is surprisingly complex especially as they are other elements of the problem not mentioned in those issues: use of multiple atlas, etc.

May I suggest in the meanwhile that you workaround the problem using:

ImGui::PushFont(fontTitle);
ImGui::Begin("Window");
ImGui::PushFont(defaultFont);
ImGui::Text("Hello!");
ImGui::PopFont();
ImGui::End();
ImGui::PopFont();

@codingoat
Copy link
Author

Using the workaround is alright with me, thank you for your help!

@ocornut
Copy link
Owner

ocornut commented Aug 20, 2024

FYI I have pushed a fix for this now: eb7201b

@ocornut ocornut reopened this Aug 20, 2024
@ocornut ocornut closed this as completed Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants