Fix several memory leaks (#88)

* m3ReallocArray: fix name typo

* fix several memory leaks
extensions
Ivan Grokhotkov 4 years ago committed by GitHub
parent a1edb9b52a
commit d7298af7c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -165,7 +165,7 @@ static const char * const c_waCompactTypes [] = { "0", "i", "I", "f", "F", "v"
#define m3Alloc(OPTR, STRUCT, NUM) m3Malloc ((void **) OPTR, sizeof (STRUCT) * (NUM)) #define m3Alloc(OPTR, STRUCT, NUM) m3Malloc ((void **) OPTR, sizeof (STRUCT) * (NUM))
#define m3RellocArray(PTR, STRUCT, NEW, OLD) m3Realloc ((PTR), sizeof (STRUCT) * (NEW), sizeof (STRUCT) * (OLD)) #define m3ReallocArray(PTR, STRUCT, NEW, OLD) m3Realloc ((PTR), sizeof (STRUCT) * (NEW), sizeof (STRUCT) * (OLD))
#define m3Free(P) { m3Free_impl((void*)(P)); P = NULL; } #define m3Free(P) { m3Free_impl((void*)(P)); P = NULL; }
# if d_m3VerboseLogs # if d_m3VerboseLogs

@ -175,6 +175,8 @@ void ReleaseRuntime (IM3Runtime i_runtime)
FreeCompilationPatches (& i_runtime->compilation); FreeCompilationPatches (& i_runtime->compilation);
m3Free (i_runtime->stack); m3Free (i_runtime->stack);
m3Free (i_runtime->memory.mallocated);
} }
@ -422,7 +424,7 @@ _ (ReadLEB_u32 (& numElements, & bytes, end));
if (endElement > offset) // TODO: check this, endElement depends on offset if (endElement > offset) // TODO: check this, endElement depends on offset
{ {
io_module->table0 = (IM3Function*)m3RellocArray (io_module->table0, IM3Function, endElement, io_module->table0Size); io_module->table0 = (IM3Function*)m3ReallocArray (io_module->table0, IM3Function, endElement, io_module->table0Size);
if (io_module->table0) if (io_module->table0)
{ {

@ -8,6 +8,8 @@
#include "m3_env.h" #include "m3_env.h"
static void Module_FreeFunctions(IM3Module i_module);
void m3_FreeModule (IM3Module i_module) void m3_FreeModule (IM3Module i_module)
{ {
if (i_module) if (i_module)
@ -15,10 +17,13 @@ void m3_FreeModule (IM3Module i_module)
m3log (module, "freeing module: %s (funcs: %d; segments: %d)", m3log (module, "freeing module: %s (funcs: %d; segments: %d)",
i_module->name, i_module->numFunctions, i_module->numDataSegments); i_module->name, i_module->numFunctions, i_module->numDataSegments);
Module_FreeFunctions (i_module);
m3Free (i_module->functions); m3Free (i_module->functions);
m3Free (i_module->imports); m3Free (i_module->imports);
m3Free (i_module->funcTypes); m3Free (i_module->funcTypes);
m3Free (i_module->dataSegments); m3Free (i_module->dataSegments);
m3Free (i_module->table0);
// TODO: free importinfo // TODO: free importinfo
m3Free (i_module->globals); m3Free (i_module->globals);
@ -27,13 +32,27 @@ void m3_FreeModule (IM3Module i_module)
} }
} }
static void Module_FreeFunctions(IM3Module i_module)
{
for (u32 i = 0; i < i_module->numFunctions; ++i)
{
IM3Function func = &i_module->functions[i];
m3Free (func->constants);
if (func->name != func->import.fieldUtf8)
{
m3Free (func->name);
}
FreeImportInfo (&func->import);
}
}
M3Result Module_AddGlobal (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported) M3Result Module_AddGlobal (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported)
{ {
M3Result result = m3Err_none; M3Result result = m3Err_none;
u32 index = io_module->numGlobals++; u32 index = io_module->numGlobals++;
io_module->globals = (M3Global *) m3RellocArray (io_module->globals, M3Global, io_module->numGlobals, index); io_module->globals = (M3Global *) m3ReallocArray (io_module->globals, M3Global, io_module->numGlobals, index);
if (io_module->globals) if (io_module->globals)
{ {
@ -57,7 +76,7 @@ M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportIn
M3Result result = m3Err_none; M3Result result = m3Err_none;
u32 index = io_module->numFunctions++; u32 index = io_module->numFunctions++;
io_module->functions = (M3Function*)m3RellocArray (io_module->functions, M3Function, io_module->numFunctions, index); io_module->functions = (M3Function*)m3ReallocArray (io_module->functions, M3Function, io_module->numFunctions, index);
if (io_module->functions) if (io_module->functions)
{ {

Loading…
Cancel
Save