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

            Bug ID: 92708
           Summary: [Issue] dynamic_cast unexpected behavior in my code
           Product: gcc
           Version: 6.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: akhilesh.k at samsung dot com
  Target Milestone: ---

Hello 

In below code I used class A and a class B which is derived from A. Now, I did 
dynamic_cast  I got segmentation fault. same behaviors I observed on ARM also. 

is this expected behavior ? or some limitation.   

As per below link seems some limitation with shared libraries. 
https://gcc.gnu.org/faq.html#dso


akhilesh.k@DELL-BUILD10:$ cat hello.c 
#include <stdio.h>
#include <iostream>
using namespace std;

class A
{
    public:
        A(){}
        virtual ~A(){};
};

class B : public A
{
    public:
        B(){}
        virtual ~B(){}
};

int main()
{
    A *pa, *pa2;
    B *pb, *pb2;
    pa = new A; 
    pb = new B; 

    delete pb;
    pa2 = dynamic_cast<A*>(pb);
    pb2 = dynamic_cast<B*>(pb);
    pb2 = dynamic_cast<B*>(pa2);
    printf("pb2 = %p\n", pb2);

    pb2 = dynamic_cast<B*>(pa);
    printf("pb2 = %p\n", pb2);

    delete pa;
    delete pb;

    return 0; 
}

akhilesh.k@DELL-BUILD10:$ g++ -g hello.c 
akhilesh.k@DELL-BUILD10:$ gdb a.out 
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) r
Starting program: /data2/2706/akhilesh.k/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b33156 in __dynamic_cast () from
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b33156 in __dynamic_cast () from
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x000000000040095d in main () at hello.c:29
(gdb)

Reply via email to