[Bug c++/56636] New: strange interaction of dynamic_cast and unique_ptr

gcc-bugs at naturalcleaningcalgary dot com gcc-bugzilla@gcc.gnu.org
Sat Mar 16 16:59:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56636

             Bug #: 56636
           Summary: strange interaction of dynamic_cast and unique_ptr
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: gcc-bugs@naturalcleaningcalgary.com


The following program produces a segfault at the dynamic_cast.  If I change
std::unique_ptr<int> to int*, or if I drop the dynamic_cast, the segfault goes
away.


#include <memory>

struct A
{
    void F() const
    {
        F_impl();
    }

    virtual std::unique_ptr<int> F_impl() const = 0;
};

template <typename Derived>
    struct A_impl : virtual A
{
    std::unique_ptr<int> F_impl() const override
    {
        dynamic_cast<const Derived &>(*this);
        return nullptr;
    }
};

struct B : virtual A {};

struct C : B, A_impl<C> {};

int main()
{
    C().F();
}


$ g++ -std=c++11 -otest test.cpp && ./test.exe

I am using GCC 4.7.2 under MinGW, configured with "--enable-libstdcxx-debug
--enable-languages=c,c++ --build=pentium4-pc-mingw32 --disable-nls
--disable-win32-registry --enable-threads".


(gdb) run
Starting program: c:\devel\page\build/test.exe
[New Thread 2356.0xa10]

Program received signal SIGSEGV, Segmentation fault.
__cxxabiv1::__dynamic_cast (src_ptr=0x4031f4, src_type=0x403180,
    dst_type=0x403160, src2dst=4)
    at ../../../../gcc-4.7.2/libstdc++-v3/libsupc++/dyncast.cc:56
56            adjust_pointer <void> (src_ptr, prefix->whole_object);
(gdb) bt
#0  __cxxabiv1::__dynamic_cast (src_ptr=0x4031f4, src_type=0x403180,
    dst_type=0x403160, src2dst=4)
    at ../../../../gcc-4.7.2/libstdc++-v3/libsupc++/dyncast.cc:56
#1  0x00401d04 in A_impl<C>::F_impl() const ()
#2  0x00401cc6 in A::F() const ()
#3  0x00401378 in main ()



More information about the Gcc-bugs mailing list