Skip to content

Commit aaea613

Browse files
authored
Fix shader uniform memory leaks (#317)
Free memory for int and float array uniforms when the uniform or shader is destroyed.
1 parent 00c6420 commit aaea613

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/minisphere/galileo.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,27 @@ shader_ref(shader_t* it)
409409
void
410410
shader_unref(shader_t* it)
411411
{
412+
struct uniform* uniform;
413+
414+
iter_t iter;
415+
412416
if (it == NULL || --it->refcount > 0)
413417
return;
414418

415419
console_log(3, "disposing shader program #%u no longer in use", it->id);
420+
421+
iter = vector_enum(it->uniforms);
422+
while ((uniform = iter_next(&iter))) {
423+
switch (uniform->type) {
424+
case UNIFORM_FLOAT_ARR:
425+
free(uniform->float_list);
426+
break;
427+
case UNIFORM_INT_ARR:
428+
free(uniform->int_list);
429+
break;
430+
}
431+
}
432+
416433
al_destroy_shader(it->program);
417434
vector_free(it->uniforms);
418435
free(it);
@@ -831,8 +848,18 @@ free_cached_uniform(shader_t* shader, const char* name)
831848

832849
iter = vector_enum(shader->uniforms);
833850
while ((uniform = iter_next(&iter))) {
834-
if (strcmp(uniform->name, name) == 0)
851+
if (strcmp(uniform->name, name) == 0) {
852+
switch (uniform->type) {
853+
case UNIFORM_FLOAT_ARR:
854+
free(uniform->float_list);
855+
break;
856+
case UNIFORM_INT_ARR:
857+
free(uniform->int_list);
858+
break;
859+
}
860+
835861
iter_remove(&iter);
862+
}
836863
}
837864
}
838865

0 commit comments

Comments
 (0)