Skip to content

Commit

Permalink
Fixed renderer text engine memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 1, 2024
1 parent 7719fcb commit 1f5c063
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/SDL_renderer_textengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct AtlasTexture
SDL_Texture *texture;
stbrp_context packer;
stbrp_node *packing_nodes;
int num_free_glyphs;
AtlasGlyph *free_glyphs;
AtlasTexture *next;
};
Expand Down Expand Up @@ -156,12 +155,27 @@ static int SDLCALL SortOperations(const void *a, const void *b)
return 0;
}

static void DestroyGlyph(AtlasGlyph* glyph)
{
if (!glyph) {
return;
}

SDL_free(glyph);
}

static void DestroyAtlas(AtlasTexture *atlas)
{
if (!atlas) {
return;
}

AtlasGlyph *next;
for (AtlasGlyph *glyph = atlas->free_glyphs; glyph; glyph = next) {
next = glyph->next;
DestroyGlyph(glyph);
}

SDL_DestroyTexture(atlas->texture);
SDL_free(atlas->packing_nodes);
SDL_free(atlas);
Expand Down Expand Up @@ -212,15 +226,14 @@ static void ReleaseGlyph(AtlasGlyph *glyph)
prev = entry;
}

++glyph->atlas->num_free_glyphs;
if (prev) {
prev->next = glyph;
} else {
glyph->atlas->free_glyphs = glyph;
}
glyph->next = entry;
} else {
SDL_free(glyph);
DestroyGlyph(glyph);
}
}
}
Expand Down Expand Up @@ -267,7 +280,6 @@ static AtlasGlyph *FindUnusedGlyph(AtlasTexture *atlas, int width, int height)
} else {
atlas->free_glyphs = glyph->next;
}
--atlas->num_free_glyphs;
++glyph->refcount;
return glyph;
}
Expand Down Expand Up @@ -502,6 +514,7 @@ static void DestroyDrawSequence(AtlasDrawSequence *data)
if (data->next) {
DestroyDrawSequence(data->next);
}
SDL_free(data->rects);
SDL_free(data->texcoords);
SDL_free(data->positions);
SDL_free(data->indices);
Expand Down Expand Up @@ -728,12 +741,20 @@ static TTF_RendererTextEngineFontData *CreateFontData(TTF_RendererTextEngineData

static void DestroyEngineData(TTF_RendererTextEngineData *data)
{
if (data) {
if (data->fonts) {
SDL_DestroyHashTable(data->fonts);
}
SDL_free(data);
if (!data) {
return;
}

if (data->fonts) {
SDL_DestroyHashTable(data->fonts);
}

AtlasTexture *next;
for (AtlasTexture *atlas = data->atlas; atlas; atlas = next) {
next = atlas->next;
DestroyAtlas(atlas);
}
SDL_free(data);
}

static void NukeFontData(const void *key, const void *value, void *unused)
Expand Down

0 comments on commit 1f5c063

Please sign in to comment.