https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103711

            Bug ID: 103711
           Summary: Virtual base destroyed twice when an exception is
                    thrown in a derived class' constructor called from a
                    delegated constructor
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pierre.mantion at cern dot ch
  Target Milestone: ---

Created attachment 52000
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52000&action=edit
preprocessed file that triggers the bug

Virtual base destroyed twice when an exception is thrown in a derived class'
constructor called from a delegated constructor:

The code below causes A’s destructor to be called twice on the same object.
This is the most I could reduce the code to have it trigger the bug.

    #include <iostream>

    int constructions = 0;
    int destructions = 0;

    struct A
    {
        A()
        {
            constructions++;
        }
        virtual ~A() {
            destructions++;
        }
    };

    struct B : public virtual A
    {
        B(int)
        {
        };

        B() : B(1)
        {
            throw -1;
        }
        virtual ~B() = default;
    };

    struct C : public B
    {
    };

    int main() {
        try
        {
            C c;
        }
        catch (int e)
        {
            std::cout << "Caught: " << e << std::endl;
        }
        std::cout << constructions << " constructions" << std::endl;
        std::cout << destructions << " destructions" << std::endl;
        return 0;
    }

Expected output:

    Caught: -1
    1 constructions
    1 destructions

Actual output:

    Caught: -1
    1 constructions
    2 destructions

This seems to be a combination of the “virtual” inheritance in B, the call to
the delegating constructor in B, and the exception thrown in B’s no arg
constructor.
Removing either of these makes the program behave as expected.
Constructing a B instead of a C works fine.

GCC latest version and ICC latest version have the same issue, but clang latest
version doesn’t.

Tested on Godbolt:
- Works/behaves as expected with Clang 13 (https://godbolt.org/z/4Yr5hWW5c)
- fails with GCC 11.2 (https://godbolt.org/z/cr8bz4hMc)
- fails with MSVC 19.29 (https://godbolt.org/z/b3Pd5PEfe)
- fails with ICC 2021.3.0 (https://godbolt.org/z/caM41fjao)
- (other compilers not tested yet).

The problematic behavior was reproduced locally with version of GCC:
% gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.0 (GCC) 

The incorrect behavior can be reproduced as well with GCC 4.8.5 (need to pass
-std=c++11 to compile in that case).
% gcc -v
Using built-in specs.
COLLECT_GCC=/acc/sys/L867/usr/bin/g++
COLLECT_LTO_WRAPPER=/nfs/cs-ccr-felab/sys/L867/usr/bin/../libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-linker-build-id --with-linker-hash-style=gnu
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin
--enable-initfini-array --disable-libgcj
--with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install
--with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install
--enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64
--build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 

Complete command to reproduce (GCC 11.1.0):

    g++ -v -save-temps -Wall -Wextra reproducer.cpp && ./a.out

Complete command to reproduce (GCC 4.8.5):

    g++ -v -save-temps -Wall -Wextra reproducer.cpp -std=c++11 && ./a.out

Reply via email to