Hi, we're working on a Lua-based project in C++, and we noticed that if Lua is 
compiled to use longjumps, C++ exceptions may leak memory.

I've narrowed it down to the following minimal example:

$ cat main.cpp
#include <setjmp.h>

static jmp_buf jb;

int main() {
        if (_setjmp(jb) == 0) {
                try {
                        throw 0;
                } catch (const int&) {
                        _longjmp(jb, 1);
                }
        }
}
$ clang -o leaky main.cpp -lstdc++
$ valgrind --leak-check=full ./leaky
==4170== Memcheck, a memory error detector
==4170== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4170== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==4170== Command: ./leaky
==4170== 
==4170== 
==4170== HEAP SUMMARY:
==4170==     in use at exit: 132 bytes in 1 blocks
==4170==   total heap usage: 2 allocs, 1 frees, 72,836 bytes allocated
==4170== 
==4170== 132 bytes in 1 blocks are possibly lost in loss record 1 of 1
==4170==    at 0x4848899: malloc (in 
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==4170==    by 0x491F073: __cxa_allocate_exception (in 
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30)
==4170==    by 0x1091BD: main (in /home/sainan/Desktop/leaky/leaky)
==4170== 
==4170== LEAK SUMMARY:
==4170==    definitely lost: 0 bytes in 0 blocks
==4170==    indirectly lost: 0 bytes in 0 blocks
==4170==      possibly lost: 132 bytes in 1 blocks
==4170==    still reachable: 0 bytes in 0 blocks
==4170==         suppressed: 0 bytes in 0 blocks
==4170== 
==4170== For lists of detected and suppressed errors, rerun with: -s
==4170== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Is there anything that can be done to free this memory, or should we just give 
up on trying to make this work?

-- Sainan

Reply via email to