From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 32F193858C60; Wed, 8 Sep 2021 02:19:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32F193858C60 From: "david.cortes.rivera at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/102237] New: longjmp leaks catched std::runtime_error Date: Wed, 08 Sep 2021 02:19:21 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 10.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: david.cortes.rivera at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Sep 2021 02:19:22 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102237 Bug ID: 102237 Summary: longjmp leaks catched std::runtime_error Product: gcc Version: 10.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: david.cortes.rivera at gmail dot com Target Milestone: --- Created attachment 51425 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D51425&action=3Dedit ii_file I am experiencing a memory leak when using long jumps from a catch block in which the jump takes to a different function. Basically, the exception obje= cts are not getting destructed. >>From what I gather from this SO question: https://stackoverflow.com/questions/69092014/c-will-an-stdruntime-error-obj= ect-leak-in-a-longjmp calling something like "catch(std::exception &e) {longjmp(jump_buffer, 1);}" should be allowed by the standard, and the exception should be destructed a= long the way. For example, the following code ***works as expected***: ------- #include #include #include void my_fun() { jmp_buf jump_buffer; if (setjmp(jump_buffer)) return; try { std::string message; message.resize(100); snprintf(&message[0], 100, "error code %d\n", 3); throw std::runtime_error(message); } catch (std::runtime_error &e) { longjmp(jump_buffer, 1); } } int main() { for (int ix =3D 0; ix < 100000; ix++) void my_fun(); return 0; } ------- ... But ***if I switch the jump buffer to be outside of the function***, it will now start leaking memory at each call: ------- #include #include #include jmp_buf jump_buffer; void my_fun() { try { std::string message; message.resize(100); snprintf(&message[0], 100, "error code %d\n", 3); throw std::runtime_error(message); } catch (std::runtime_error &e) { longjmp(jump_buffer, 1); } } void call_myfun() { if (setjmp(jump_buffer)) return; my_fun(); } int main() { for (int ix =3D 0; ix < 100000; ix++) call_myfun(); return 0; } ------- Setup info: - AMD Ryzen 7 2700. - Debian linux (sid) - gcc --version: gcc (Debian 10.3.0-9) 10.3.0 The source file is compiled with the default options (g++ file.cpp). The le= ak is detected by valgrind-3.16.1. Attached is the .ii file.=