Skip to content

Commit 0fbd181

Browse files
committed
Fix memory leaks
1 parent a2d0e4e commit 0fbd181

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pdlua.c

+2
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,8 @@ static int pdlua_object_free(lua_State *L)
18541854

18551855
if (o)
18561856
{
1857+
pdlua_gfx_free(&o->gfx);
1858+
18571859
if(o->in)
18581860
{
18591861
for (i = 0; i < o->inlets; ++i) inlet_free(o->in[i]);

pdlua_gfx.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ static int free_path(lua_State* L);
8383

8484
static void pdlua_gfx_clear(t_pdlua *obj, int layer, int removed); // only for pd-vanilla, to delete all tcl/tk items
8585

86+
void pdlua_gfx_free(t_pdlua_gfx *gfx) {
87+
for(int i = 0; i < gfx->num_layers; i++)
88+
{
89+
free(gfx->layer_tags[i]);
90+
}
91+
free(gfx->layer_tags);
92+
if(gfx->transforms) freebytes(gfx->transforms, gfx->num_transforms * sizeof(gfx_transform));
93+
}
94+
8695
// Trigger repaint callback in lua script
8796
void pdlua_gfx_repaint(t_pdlua *o, int firsttime) {
8897
#if !PLUGDATA
@@ -775,7 +784,7 @@ static int gfx_initialize(t_pdlua *obj)
775784
gfx->transforms = NULL;
776785
gfx->num_transforms = 0;
777786
gfx->num_layers = 0;
778-
gfx->layer_tags = malloc(sizeof(char*));
787+
gfx->layer_tags = NULL;
779788

780789
pdlua_gfx_repaint(obj, 0);
781790
return 0;
@@ -839,7 +848,11 @@ static int start_paint(lua_State* L) {
839848
if(layer >= gfx->num_layers)
840849
{
841850
int new_num_layers = layer + 1;
842-
gfx->layer_tags = resizebytes(gfx->layer_tags, sizeof(char*) * gfx->num_layers, sizeof(char*) * new_num_layers);
851+
if(gfx->layer_tags)
852+
gfx->layer_tags = resizebytes(gfx->layer_tags, sizeof(char*) * gfx->num_layers, sizeof(char*) * new_num_layers);
853+
else
854+
gfx->layer_tags = malloc(sizeof(char*));
855+
843856
gfx->num_layers = new_num_layers;
844857
gfx->layer_tags[layer] = malloc(64);
845858
snprintf(gfx->layer_tags[layer], 64, ".l%i%lx", layer, (long)obj);

0 commit comments

Comments
 (0)