mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-08 19:02:56 +00:00
AddFontFromMemoryTTF invalidate font->ConfigData pointer of previous fonts #6825
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#6825
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I can’t figure it out, but after calling AddFontFromMemoryTTF(), the pointer to the previously loaded ImFont font is reset to zero and setting ImFontConfig_.FontDataOwnedByAtlas = false - has no effect on this.
Can you provide a code sample?
You can also just call the method directly instead of storing it in a pointer unless you have separate fonts. I would also make sure that your character array is in a separate header file and that you are only including it once.
If you are using a pointer make sure you follow the syntax:
ImFont* fontPointer = io.Fonts->AddFontFrommemoryTTF(arr, sizeof(arr), 3.f);
(I think these are the correct parameters im not sure)
This happens when I load two fonts into a combo font at once and then load those two fonts into another combo font again - at which point Imgui simply deletes the loaded fonts.
This is just some kind of cruelty.
You are making a mistake and the only way to get help is to share an actual verifiable example showing the issue.
Please stop opening issues that are ignoring our Issue Opening Guidelines:
https://github.com/ocornut/imgui/blob/master/docs/CONTRIBUTING.md
Please show an example with a video and some source code.
Thanks :)
My apologies.
This is pure Imgui code (no backend) that is guaranteed to cause an error, or more precisely AddFontFromMemoryTTF() just takes and removes the previously loaded font:
Which exact call to the AddFontFromMemoryTTF function deletes the font was indicated in the comment to the code against the function.:
This Imgui code causes an error:
Assertion failed: font_cfg->FontData != 0 && font_cfg->FontDataSize > 0, file imgui_draw.cpp, line 2128Comment in the code.
Moreover, if at the beginning of the code I delete the line for loading the default Imgui Font, then the code completes successfully. I can't figure out what's wrong :(
You are passing a value of 0 for font_data_size and it is asserting because of that.
You shouldn’t need to call Build() more than once at the end.
But I don’t have a variable in the code called font_data_size to which I pass 0.
I call Build() exactly once after adding the font.
Adding fonts in code looks like I'm adding them all at once, but loading fonts can happen at different times while the application is running, so I still have to call Build() every time - right?
I'm talking about the Imgui function AddFontFromMemoryTTF - deletes the downloaded font. It just takes it and deletes it and because of this the application crashes.
Showed it. And ? :)
You are regularly passing values that you have never set, eg when you are passing ImFont_p_22->ConfigData->FontDataSize to the function. This is likely to be 0 you never set that function.
This becomes a whole different problem if you load during application run. It needs to always be done before NewFrame(). But there’s no need to call Build() more than once in the code you posted which is the code we are discussing now.
Are you using a debugger to confirm this or are you just making wild guesses? You should always be using a debugger to confirm your data and code behavior.
For some reason you don't want to hear me.
Firstly, I wrote that if I remove the loading of the default Imga font at the very beginning, then ALL the rest of the code works, that is, ImFont_p_22->ConfigData->FontDataSize is NOT equal to zero.
Secondly: WHY IN YOUR opinion ImFont_p_22->ConfigData->FontDataSize MUST be zero??
If I loaded the Greek.TTF font into ImFont_p_22?? This is, to put it mildly, a very strange comment on your part.
I’m not a complete fool, of course I used debugging, I wrote from the BEGINNING and even in the title of the topic that the Imgui AddFontFromMemoryTTF function ITSELF deletes previously loaded fonts. I even indicated in the code with a comment after which particular call to the AddFontFromMemoryTTF function - this deletion occurs.
You would be surprised how many well-intended people are accidentally making false claims, then we have to work and dig into their mistake for them. I'm not saying you are making a false claim, but in order to get the free help you are receiving here you need to be putting every effort to prove your claim.
If you said "I used a debugger and noticed that between point XX and point XX this value changes in a way that seems incorrect" that would immediately convey what you tried and understood better. Otherwise I have to guess.
Moreoever, I know for a fact that we don't "delete" fonts randomly.
And, right now we cannot easy test your code as it depends on files/ranges we don't have, so you are not making it easier for us.
That was my honest intuition reading the code because you are never setting this value, and well, this is what one of the two possible things the assert is triggering for.
it looks like you are accessing some internal members on the assumption that internal loading code will set them for you.
So I looked into it and here's the issue:
Technically none of the members in ImFont are for public consumption but I do realize this isn't explicitly written down.
You are accessing through those pointers after calling AddFontXXX and before BuildXXX so this is faulty and causing your issue.
I'm going to make a change now to prevent or detect that better.
I have pushed a change
6addf28to make this undefined behavior (access ImFont::ConfigData between Add and Build) better defined.Note that two issues remains in your code:
I do realize that the lifetime and ownership issues are confusing in the font loading API.
I've been hesitant to redesign/refactor them yet because I don't have all elements I need for a full "V2" font api yet.
Now I understand, thank you very much.
Closing as answered + some changes to facilitate this.
Hope to get back to the ownership mess soon.