Skip to content

Constructed array elements are not destroyed when value-initialization of an array member throws #604

Backlog

Description

@nv-cmeerw

Discussed in #554

Originally posted by lexx42xxel November 18, 2026

Issue Summary

For G g{}; where G has a member P a[4] and P has a nontrivial default constructor and a destructor, a throwing element constructor leaks the elements already constructed.

Reproducing Source Code

extern "C" int printf(const char *, ...);
int ctors, dtors;
struct E {
  E() { if (++ctors == 3) throw 1; }
  ~E() { ++dtors; }
};
struct P { int i; E e; };
struct G { P a[4]; };
int main() {
  try { G g{}; } catch (int) {}
  printf("constructed %d, destroyed %d (expected equal)\n", ctors - 1, dtors);
  return ctors - 1 != dtors;
}

Command-line Options

eccp --c++17 --g++ --exceptions t.cpp -o t && ./t

Type of Issue

[Back End] There is a problem with the generated C code.

Additional Details

Version: EDG C/C++ front end 7.0, main at commit
6a6c7f2d6bd8027f1b0c90877b4ba222fbff6642 (2026-11-15).

Actual output:

constructed 2, destroyed 0 (expected equal)

Expected: constructed 2, destroyed 2

The elements are initialized by a generated helper routine without cleanups for already-constructed
elements (or with a cleanup for only one), cleanups of preceding members are lost, and the
helper's EH epilogue is missing. Also affected: default member initializers,
multidimensional/nested arrays and new G{}.

The program exits nonzero when the bug is present. A fix with a regression
test is ready (branch fix/vla-array-init-lowering); a PR will reference this issue.

AddressSanitizer

AddressSanitizer detects the same bug in this variant, which has no counters or output of its own:

int ctors;
struct E {
  int *p;
  E() : p(new int(0)) { if (++ctors == 3) { delete p; throw 1; } }
  ~E() { delete p; }
};
struct P { int i; E e; };
struct G { P a[4]; };
int main() {
  try { G g{}; } catch (int) {}
  return 0;
}
eccp --c++17 --g++ --exceptions -g --c_to_obj_option -fsanitize=address t.cpp -o t && ./t

Report on main (6a6c7f2), addresses elided:

ERROR: LeakSanitizer: detected memory leaks
    #0 0x... in operator new(unsigned long) (/lib64/libasan.so.8+0xe7a5b)
    #1 0x... in E::E() value-init-array-member-throw.cpp:4
    #2 0x... in __T1 value-init-array-member-throw.cpp:10
    #3 0x... in main value-init-array-member-throw.cpp:10
SUMMARY: AddressSanitizer: 8 byte(s) leaked in 2 allocation(s).

No report with the fix, nor with g++ (-fsanitize=address).

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Reproducing (Non-standard) Configuration

No response

I acknowledge that:

  • I have checked open issues and existing discussions before filing this report.
  • The reproducing source code that I have provided does not depend on headers that I have not included with my submission.
  • I have reduced the reproducing source code to the best of my ability.
  • I have selected at least one standard configuration or uploaded a non-standard configuration dump.
  • I understand code shared via this form is NOT confidential.
  • I have permission to share the reproducing source code and grant permission for the project to use the source code (or derivatives of the source code) to create tests.
  • I agree that any tests created using the reproducing source code (or deriatives of it) will be licensed under project license unless I included a LICENSE.txt file in my .zip or .tar.gz upload. If I included a LICENSE.txt file, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions