Skip to content

Commit a130b10

Browse files
ksh8281bbrto21
authored andcommitted
Implement Global::finalizeGC to prevent memory leak
Signed-off-by: Seonghyun Kim <[email protected]>
1 parent fb2ad1e commit a130b10

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

src/api/EscargotPublic.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ void Globals::initialize(PlatformRef* platform)
269269

270270
void Globals::finalize()
271271
{
272+
RELEASE_ASSERT(!!g_globalsInited);
273+
g_globalsInited = false;
272274
// finalize global value or context including thread-local variables
273275
// this function should be invoked once at the end of the program
274-
RELEASE_ASSERT(!!g_globalsInited);
275276
ThreadLocal::finalize();
276277

277278
// Global::finalize should be called at the end of program
278279
// because it holds Platform which could be used in other Object's finalizer
279280
Global::finalize();
280-
g_globalsInited = false;
281281
}
282282

283283
bool Globals::isInitialized()
@@ -296,11 +296,12 @@ void Globals::initializeThread()
296296

297297
void Globals::finalizeThread()
298298
{
299+
RELEASE_ASSERT(!!g_globalsInited);
300+
g_globalsInited = false;
301+
Global::finalizeGC();
299302
// finalize thread-local variables
300303
// this function should be invoked once at the end of sub-thread
301-
RELEASE_ASSERT(!!g_globalsInited);
302304
ThreadLocal::finalize();
303-
g_globalsInited = false;
304305
}
305306

306307
bool Globals::supportsThreading()

src/runtime/FinalizationRegistryObject.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "interpreter/ByteCode.h"
3030
#include "parser/CodeBlock.h"
3131
#include "util/Util.h"
32+
#include "api/EscargotPublic.h"
3233

3334
namespace Escargot {
3435

@@ -197,7 +198,7 @@ void FinalizationRegistryObject::finalizer(PointerValue* self, void* data)
197198
}
198199
}
199200

200-
if (!wasCallbackDeleted) {
201+
if (!wasCallbackDeleted && Globals::isInitialized()) {
201202
try {
202203
ExecutionState tempState(item->source->m_realm);
203204
Value argv = item->heldValue;

src/runtime/Global.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ void Global::finalize()
7575
std::vector<Waiter*>().swap(g_waiter);
7676
#endif
7777

78-
GC_gcollect_and_unmap();
79-
GC_gcollect_and_unmap();
80-
GC_gcollect_and_unmap();
81-
GC_invoke_finalizers();
78+
Global::finalizeGC();
8279

8380
delete g_platform;
8481
g_platform = nullptr;

src/runtime/Global.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ class Global {
4747
public:
4848
static void initialize(Platform* platform);
4949
static void finalize();
50+
static void finalizeGC()
51+
{
52+
GC_register_mark_stack_func([]() {});
53+
GC_gcollect_and_unmap();
54+
GC_gcollect_and_unmap();
55+
GC_gcollect_and_unmap();
56+
GC_invoke_finalizers();
57+
GC_register_mark_stack_func(nullptr);
58+
}
5059

5160
static Platform* platform();
5261
#if defined(ENABLE_ATOMICS_GLOBAL_LOCK)

src/wasm/WASMObject.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "wasm/WASMObject.h"
3030
#include "wasm/WASMValueConverter.h"
3131
#include "wasm/ExportedFunctionObject.h"
32+
#include "api/EscargotPublic.h"
3233

3334
// represent ownership of each object
3435
// object marked with 'own' should be deleted in the current context
@@ -138,6 +139,9 @@ WASMMemoryObject::WASMMemoryObject(ExecutionState& state, Object* proto, wasm_me
138139

139140
addFinalizer([](PointerValue* obj, void* data) {
140141
WASMMemoryObject* self = (WASMMemoryObject*)obj;
142+
if (!Globals::isInitialized()) {
143+
return;
144+
}
141145
if (!wasm_memory_is_shared(self->memory())) {
142146
self->buffer()->asArrayBufferObject()->detachArrayBuffer();
143147
}

0 commit comments

Comments
 (0)