Skip to content

Commit

Permalink
make sure to free fonts & images
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasvlevi committed Aug 10, 2024
1 parent 18137bf commit 1262aa2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/lu5_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int lu5_load_font(lu5_State *l5, lu5_font **fontId, const char *fontPath)
font->textures[character] = texture;
}

// If fontId pointer is valid,
// If fontId pointer is invalid,
if (fontId != NULL)
// set the value to the next fontId
*fontId = font;
Expand Down Expand Up @@ -146,17 +146,24 @@ void lu5_render_text(const char *text, float x, float y, float fontSize, lu5_fon

void lu5_close_font(lu5_font *font)
{
for (int i = 0; i < 128; i++) {
glDeleteTextures(1, &font->textures[i]);
if (font != NULL) {
for (int i = 0; i < 128; i++) {
glDeleteTextures(1, &font->textures[i]);
}
FT_Done_Face(font->face);
}
FT_Done_Face(font->face);
}

void lu5_close_fonts(lu5_State *l5)
{
// Clear default font
lu5_close_font(l5->font_default);
l5->font_default = NULL;

// Clear dangling reference
l5->style.font_current = NULL;

// Clear all fonts
lu5_list_iter_close(l5->fonts, (void(*)(void*))lu5_close_font);
l5->fonts = NULL;
}
7 changes: 5 additions & 2 deletions src/lu5_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ void lu5_render_image(lua_State* L, GLuint texture, double x, double y, double w

void lu5_close_image(lu5_image *image)
{
glDeleteTextures(1, &(image->texture));
free(image);
if (image != NULL) {
glDeleteTextures(1, &(image->texture));
free(image);
}
}

void lu5_close_images(lu5_State *l5)
{
lu5_list_iter_close(l5->images, (void (*)(void*))lu5_close_image);
l5->images = NULL;
}

0 comments on commit 1262aa2

Please sign in to comment.