public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pierre.mantion at cern dot ch" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/103711] New: Virtual base destroyed twice when an exception is thrown in a derived class' constructor called from a delegated constructor
Date: Tue, 14 Dec 2021 15:45:48 +0000	[thread overview]
Message-ID: <bug-103711-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2021-12-14 15:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-14 15:45 pierre.mantion at cern dot ch [this message]
2021-12-14 15:58 ` [Bug c++/103711] " pinskia at gcc dot gnu.org
2021-12-15  9:49 ` sebastian.redl at getdesigned dot at
2021-12-15 21:03 ` redi at gcc dot gnu.org
2021-12-15 21:04 ` redi at gcc dot gnu.org
2021-12-15 21:05 ` redi at gcc dot gnu.org
2022-01-05 23:10 ` jason at gcc dot gnu.org
2022-01-07  0:26 ` cvs-commit at gcc dot gnu.org
2022-01-07  0:33 ` jason at gcc dot gnu.org
2022-01-28  4:38 ` cvs-commit at gcc dot gnu.org
2022-01-28  4:39 ` jason at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-103711-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).