dazKind / linc_bgfx

Haxe hxcpp externs for bgfx
MIT License
12 stars 0 forks source link

NanoVG (fontstash.h, fonsFreeFontMem): Cannot unload fonts of which the index is higher than nfonts. #3

Open mikaib opened 3 months ago

mikaib commented 3 months ago

afbeelding

Description There is a problem with NanoVG where certain fonts are unable to be unloaded. Let's say I load a font called font1, then load another font called font2. At this point stash->nfonts will be 2 If I now unload font1 stash->nfonts will be 1 again. This means that font2 cannot be unloaded as there is a check in place (idx >= stash->nfonts) I've confirmed this is the problem by trying to unload them in reverse order (font2, font1 instead of font1, font2) which made the problems disappear.

To Reproduce Steps to reproduce the behavior:

  1. Load 2 or more fonts
  2. Unload the first font
  3. Try to unload the second font, the call to do this will return false.

Expected behavior When you attempt step 3 of the reproduction steps, you should be able to unload the font and the function should return true.

Proposed solution Instead of checking if idx >= stash->nfonts is true, possibly check if the font at the specified index is a nullptr, with perhaps some additional sanity-checks like idx >= FONS_INIT_FONTS

Additional details Outside of the code added to this fork to free the font, there is also a problem with finding fonts. As it uses a for-loop for finding the fonts, where sometimes a font might be outside of the range of the font

dazKind commented 3 months ago

I updated nanovg and fontstash. There are some more functions using the fontindex now. But it still is suboptimal since the stash doesnt really support releasing the fonts and doesnt allow for a proper lifecycle. Fontstash is also super ugly so im pondering a rewrite atm.