Skip to content

Commit fe7d36f

Browse files
authored
Fix __cxa_find_matching_catch's memory leak (emscripten-core#8947)
`__cxa_find_matching_catch` allocated a buffer of size 4 using `malloc` only when it was first called and reused it through the entire program run. And this malloc'ed memory wasn't freed. Because this is only allocated once, I changed it to a static allocation using `makeStaticAlloc`. Fixes emscripten-core#8919.
1 parent f6a2515 commit fe7d36f

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/library_exceptions.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,16 @@ var LibraryExceptions = {
279279

280280
var pointer = {{{ exportedAsmFunc('___cxa_is_pointer_type') }}}(throwntype);
281281
// can_catch receives a **, add indirection
282-
if (!___cxa_find_matching_catch.buffer) ___cxa_find_matching_catch.buffer = _malloc(4);
283282
#if EXCEPTION_DEBUG
284283
out("can_catch on " + [thrown]);
285284
#endif
286-
{{{ makeSetValue('___cxa_find_matching_catch.buffer', '0', 'thrown', '*') }}};
287-
thrown = ___cxa_find_matching_catch.buffer;
285+
#if DISABLE_EXCEPTION_CATCHING == 1
286+
var buffer = 0;
287+
#else
288+
var buffer = {{{ makeStaticAlloc(4) }}};
289+
#endif
290+
{{{ makeSetValue('buffer', '0', 'thrown', '*') }}};
291+
thrown = buffer;
288292
// The different catch blocks are denoted by different types.
289293
// Due to inheritance, those types may not precisely match the
290294
// type of the thrown object. Find one which matches, and

0 commit comments

Comments
 (0)