From 37e42eb60b74318077eabe7cc8c2637623961da3 Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 29 Aug 2023 15:27:22 -0500 Subject: [PATCH] fixed minor memory leak in cparse we keep track of internal values used by the parser by pushing them onto the stack and popping them off once complete. --- src/cparse.c | 15 ++++++++++++++- src/cvm.c | 1 - 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cparse.c b/src/cparse.c index c7099a3..8b8ec18 100644 --- a/src/cparse.c +++ b/src/cparse.c @@ -65,6 +65,8 @@ typedef struct CObjString *module; // name of the module CToken current; CToken previous; // token right after the current token + int workingStackCount; // we push CValues of objects we need onto the stack so the garbage collector can see them. + // this is the count of those values so we'll know how many to pop off when we're done } CParseState; typedef enum @@ -107,6 +109,12 @@ static CObjFunction *endCompiler(CParseState *pstate); // ================================================================ [FRONT END/TALK TO LEXER] +static void keepTrackOf(CParseState *pstate, CValue val) +{ + pstate->workingStackCount++; + cosmoV_pushValue(pstate->state, val); +} + static void initCompilerState(CParseState *pstate, CCompilerState *ccstate, FunctionType type, CCompilerState *enclosing) { @@ -122,7 +130,7 @@ static void initCompilerState(CParseState *pstate, CCompilerState *ccstate, Func ccstate->function = cosmoO_newFunction(pstate->state); ccstate->function->module = pstate->module; - cosmoV_pushRef(pstate->state, (CObj *)ccstate->function); + keepTrackOf(pstate, cosmoV_newRef((CObj *)ccstate->function)); ccstate->loop.scope = -1; // there is no loop yet @@ -151,13 +159,18 @@ static void initParseState(CParseState *pstate, CCompilerState *ccstate, CState pstate->state = s; pstate->compiler = ccstate; pstate->module = cosmoO_copyString(s, module, strlen(module)); + pstate->workingStackCount = 0; + keepTrackOf(pstate, cosmoV_newRef((CObj *)pstate->module)); initCompilerState(pstate, ccstate, FTYPE_SCRIPT, NULL); // enclosing starts as NULL } static void freeParseState(CParseState *pstate) { cosmoL_cleanupLexState(pstate->state, &pstate->lex); + + // pop our working values off the stack + cosmoV_setTop(pstate->state, pstate->workingStackCount); } static void errorAt(CParseState *pstate, CToken *token, const char *format, va_list args) diff --git a/src/cvm.c b/src/cvm.c index aec8b49..c7bc95a 100644 --- a/src/cvm.c +++ b/src/cvm.c @@ -57,7 +57,6 @@ COSMO_API bool cosmoV_undump(CState *state, cosmo_Reader reader, const void *ud) COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *name) { CObjFunction *func; - CPanic *panic = cosmoV_newPanic(state); if (cosmoV_protect(panic)) {