Skip to content

Commit

Permalink
- fixed memory leak in ZScript backend.
Browse files Browse the repository at this point in the history
This allocated some memory and never freed it again. A TArray would have been better - but since we know the maximum size is 4 we may just use a static array here to keep things as efficient as possible.
  • Loading branch information
coelckers committed Nov 24, 2022
1 parent 65a26d6 commit ffdd0a1
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/common/scripting/backend/codegen.cpp
Expand Up @@ -723,16 +723,17 @@ ExpEmit FxVectorValue::Emit(VMFunctionBuilder *build)
{
if (e) vectorElements++;
}
assert(vectorElements > 0);
assert(vectorElements > 0 && vectorElements <= 4);

ExpEmit* tempVal = (ExpEmit*)calloc(vectorElements, sizeof(ExpEmit));
ExpEmit* val = (ExpEmit*)calloc(vectorElements, sizeof(ExpEmit));
// We got at most 4 elements
ExpEmit tempVal[4];
ExpEmit val[4];

// Init ExpEmit
for (int i = 0; i < vectorElements; ++i)
{
new(tempVal + i) ExpEmit(xyzw[i]->Emit(build));
new(val + i) ExpEmit(EmitKonst(build, tempVal[i]));
tempVal[i] = ExpEmit(xyzw[i]->Emit(build));
val[i] = EmitKonst(build, tempVal[i]);
}

{
Expand Down

0 comments on commit ffdd0a1

Please sign in to comment.