-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
AddFontFromMemoryTTF invalidate font->ConfigData pointer of previous fonts #6825
Comments
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); |
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. |
You are making a mistake and the only way to get help is to share an actual verifiable example showing the issue. |
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:
This Imgui code causes an error:
|
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. |
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.
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. 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. I'm going to make a change now to prevent or detect that better. |
…s ConfigData pointers prior to building again. (#6825)
I have pushed a change 6addf28 to 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. |
Now I understand, thank you very much. |
Closing as answered + some changes to facilitate this. Hope to get back to the ownership mess soon. |
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.
The text was updated successfully, but these errors were encountered: