Skip to content

Subobjects of a thrown aggregate are destroyed twice #603

Backlog

Description

@nv-cmeerw

Discussed in #553

Originally posted by lexx42xxel November 18, 2026

Issue Summary

throw H{E(7)}; where H is an aggregate of class members with destructors destroys the members during unwinding and again when the exception object is destroyed.

Reproducing Source Code

extern "C" int printf(const char *, ...);
int live;
struct E {
  E(int = 0) { ++live; }
  E(const E &) { ++live; }
  ~E() { --live; }
};
struct H { E a, b; };
int main() {
  try { throw H{E(7)}; } catch (...) {}
  printf("live E objects after catch: %d (expected 0)\n", live);
  return live != 0;
}

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:

live E objects after catch: -1 (expected 0)

Expected: live E objects after catch: 0

The cleanups for partially constructed subobjects of the aggregate initialization are still active
when the exception is thrown. They must be removed from the cleanup state once the exception object
is completely initialized.

The program exits nonzero when the bug is present. A fix with a regression
test is ready (branch fix/throw-aggregate-subobject-cleanup); a PR will reference this issue.

AddressSanitizer

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

struct E {
  int *p;
  E(int v = 0) : p(new int(v)) {}
  E(const E &o) : p(new int(*o.p)) {}
  ~E() { delete p; }
};
struct H { E a, b; };
int main() {
  try { throw H{E(7)}; } catch (...) {}
  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: AddressSanitizer: attempting double-free on 0x... in thread T0:
   #0 0x... in operator delete(void*, unsigned long) (/lib64/libasan.so.8+0xe89bb)
    #1 0x... in E::~E() throw-aggregate-subobject-cleanup.cpp:5
    #2 0x... in H::~H() throw-aggregate-subobject-cleanup.cpp:7
    #3 0x... in _INTERNAL_7_throw_c_a97394b4::destroy_thrown_object(a_throw_stack_entry*)
lib_src/throw.c:1195
    #4 0x... in __free_thrown_object lib_src/throw.c:1889
    #5 0x... in __destroy_exception_object lib_src/throw.c:1950
    #6 0x... in main throw-aggregate-subobject-cleanup.cpp:9
SUMMARY: AddressSanitizer: double-free throw-aggregate-subobject-cleanup.cpp:5 in E::~E()

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.

Activity

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