erkkah / tigr

TIGR - the TIny GRaphics library for Windows, macOS, Linux, iOS and Android.
Other
745 stars 44 forks source link

Crash at using external font #57

Closed milen-prg closed 1 year ago

milen-prg commented 1 year ago

Windows 10 Pro 64 bit, both MINGW64 and MSVC 2022 are tried, all updated to the last version.

I apologize, but try to use the new tigrfont-v1.0.1 (Windows exe, precompiled) to generate custom font. This is the minimal code:

`

include "tigr.h"

int main(int argc, char argv[]){ Tigr screen = tigrWindow(320, 240, "Proba1", 0);

Tigr* fontImage = tigrLoadImage("../font/JuliaMono-Bold18.png");
TigrFont * font = tigrLoadFont(fontImage, TCP_UTF32);

while (!tigrClosed(screen) && !tigrKeyDown(screen, TK_ESCAPE)){
    tigrClear(screen, tigrRGB(0x80, 0x90, 0xa0));
    tigrPrint(screen, font, 120, 110, tigrRGB(0xff, 0xff, 0xff), "Hello, Проба.");
    tigrUpdate(screen);
}

tigrFreeFont(font);
tigrFree(fontImage);
tigrFree(screen);

return 0;

} `

It WORKS fine, all, the Cyrillic symbols are displayed properly, until the the window is closed and exactly at the row:

tigrFreeFont(font);

the program CRASHES. If I comment this row, the program closes normally, but I check - then there is memory leakage as expected.

Please for advice, may be I do something wrong? I try to change the three "free" rows order, but this did not change anything, the program always crashes exactly on the command: tigrFreeFont(font);

erkkah commented 1 year ago

Thanks for the report. I'll take a look

milen-prg commented 1 year ago

This is my font, you can try directly with it, but I thing it is not the problem. JuliaMono-Bold18.zip

JettMonstersGoBoom commented 1 year ago

tigrFreeFont releases the bitmap, tigrFree(fontImage); should not be needed ?

milen-prg commented 1 year ago

@JettMonstersGoBoom, indeed, if I comment the tigrFree(fontImage);, but keep the tigrFreeFont(font);, the program do not crashes and there is no memory leaks. But this is not intuitive, I would never guess, where and when must free the bitmaps?

JettMonstersGoBoom commented 1 year ago

I'd expect tigrFreeFont to free all memory used by the font. all other bitmaps just use tigrFree.

erkkah commented 1 year ago

Thanks, @JettMonstersGoBoom. Yes, the font created by tigrLoadFont takes ownership of the provided font sheet bitmap. I'll add a note about that in the api docs.