Skip to content

Commit

Permalink
Fix shaderc leaks (#1313)
Browse files Browse the repository at this point in the history
* Fix fcpp memory leaks

* Fix glsl_optimizer leaks
  • Loading branch information
Lectem authored and bkaradzic committed Jan 13, 2018
1 parent 9be9848 commit 7960b42
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
11 changes: 9 additions & 2 deletions 3rdparty/fcpp/cpp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int fppPreProcess(struct fppTag *tags)
{
size_t i=0;
ReturnCode ret; /* cpp return code */
int retVal; /* fppPreProcess return code */
struct Global *global;

global=(struct Global *)malloc(sizeof(struct Global));
Expand Down Expand Up @@ -144,10 +145,16 @@ int fppPreProcess(struct fppTag *tags)
}
fflush(stdout);
// BK - fclose(stdout);
delalldefines(global);

retVal = IO_NORMAL;
if (global->errors > 0 && !global->eflag)
return(IO_ERROR);
return(IO_NORMAL); /* No errors or -E option set */
retVal = IO_ERROR;
free(global->tokenbuf);
free(global->functionname);
free(global->spacebuf);
free(global);
return retVal; /* No errors or -E option set */
}

INLINE FILE_LOCAL
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/fcpp/cpp3.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ ReturnCode initdefines(struct Global *global)
return(FPP_OK);
}

void deldefines(struct Global *global)
void delbuiltindefines(struct Global *global)
{
/*
* Delete the built-in #define's.
Expand Down
25 changes: 24 additions & 1 deletion 3rdparty/fcpp/cpp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,35 @@ DEFBUF *defendel(struct Global *global,
}


void delalldefines(struct Global *global)
{
/*
* Delete all the defines in the tables and free memory
*/

DEFBUF *dp;
DEFBUF *prevp;
int i;

for (i = 0; i < SBSIZE; ++i)
{
prevp = global->symtab[i];
while ((dp = prevp) != (DEFBUF *)NULL) {
prevp = dp->link;
free(dp->repl); /* Free the replacement */
free((char *)dp); /* Free the symbol */
}
global->symtab[i] = NULL;
}
}


void outdefines(struct Global *global)
{
DEFBUF *dp;
DEFBUF **syp;

deldefines(global); /* Delete built-in #defines */
delbuiltindefines(global); /* Delete built-in #defines */
for (syp = global->symtab; syp < &global->symtab[SBSIZE]; syp++) {
if ((dp = *syp) != (DEFBUF *) NULL) {
do {
Expand Down
3 changes: 2 additions & 1 deletion 3rdparty/fcpp/cppadd.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ void dumpadef(char *, register DEFBUF *);
#endif
ReturnCode openfile(struct Global *,char *);
int cget(struct Global *);
void deldefines(struct Global *);
void delbuiltindefines(struct Global *);
void delalldefines(struct Global *);
char *Getmem(struct Global *, int);
ReturnCode openinclude(struct Global *, char *, int);
ReturnCode expstuff(struct Global *, char *, char *);
4 changes: 4 additions & 0 deletions 3rdparty/glsl-optimizer/src/glsl/glsl_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ struct glslopt_shader
{
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++)
ralloc_free(whole_program->_LinkedShaders[i]);
for(GLuint i =0;i< whole_program->NumShaders;i++)
ralloc_free(whole_program->Shaders[i]);
ralloc_free(whole_program->Shaders);
ralloc_free(whole_program->InfoLog);
ralloc_free(whole_program);
ralloc_free(rawOutput);
ralloc_free(optimizedOutput);
Expand Down
2 changes: 2 additions & 0 deletions tools/shaderc/shaderc_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace bgfx { namespace glsl

printCode(_code.c_str(), line, start, end, column);
fprintf(stderr, "Error: %s\n", log);
glslopt_shader_delete(shader);
glslopt_cleanup(ctx);
return false;
}
Expand Down Expand Up @@ -298,6 +299,7 @@ namespace bgfx { namespace glsl
writeFile(disasmfp.c_str(), optimizedShader, shaderSize);
}

glslopt_shader_delete(shader);
glslopt_cleanup(ctx);

return true;
Expand Down

0 comments on commit 7960b42

Please sign in to comment.