Closed olliwang closed 2 years ago
@memononen Thanks for your hint. After examining the code again. I found that the memory leaks issue is actually caused by not releasing font images properly in nvgEndFrame()
.
For example, when the NVG_MAX_FONTIMAGES
number of font images are allocated all at NVG_MAX_FONTIMAGE_SIZE
. If the next frame uses only 2 font images. nvgEndFrame()
will set the last two font images to 0
directly without calling nvgDeleteImage()
.
I've updated to code to initialize all font images to 0
in nvgCreateInternal()
as well. Currently this is done only in nvgDeleteInternal()
. Also, whenever a font image is deleted in nvgEndFrame()
, the image handle resets to 0 immediately. That way, we can make sure the clear all images after j
loop not reset image handle without releasing the corresponded image.
@memononen Yeah, your solution is much better and cleaner. Just updated the code. :)
one more update, then we're good to go :)
Oops! How can I forget that. Just updated the code. :D
Thank you for the fix!
Cheers!
In my local version I implemented a handler for fonsSetErrorCallback
that reallocates the image on error code FONS_ATLAS_FULL
.
This way I got rid of the fontImages
array entirely.
And I was also able to remove these checks:
https://github.com/memononen/nanovg/blob/397f3300bc14bdb856ec429c71c345ab0340b0a6/src/nanovg.c#L2494-L2505
There was a bit more code that got a lot simpler that way
The reason for the complexity is that some of the backends cache the data for a whole frame. So if you fiddle with the textures there, then you might render garbage.
This commit fixes the memory leak issue caused by not handling the situation where the number of allocated font images meets
NVG_MAX_FONTIMAGES
.In this implementation of handling such situation. All existing font images will be released and a new one with the
NVG_MAX_FONTIMAGE_SIZE
will be created.