Gdb issues with virtual inheritance

Discussion in 'C++' started by Mahesh, May 1, 2007.

  1. Mahesh

    Mahesh Guest

    Hi,
    I encounted some problems using GDB with classes that use virtual
    inheritance. To illustrate this issue, I have created a simple test
    case.
    Here it goes.




    #include <iostream>
    using namespace std;


    ///////////////////////////////////////////////////////
    class ViBase
    {
    public:

    ViBase(const char* s)
    : m_s(s)
    {
    cout << "- ViBase constructor called: 0x" << hex << this
    << " s=" << s << dec << endl << endl;
    };

    const char* GetS() const
    {
    return m_s;
    };

    private:

    const char* m_s;
    };


    ///////////////////////////////////////////////////////
    class Base
    : virtual public ViBase
    {
    public:

    Base(const int k)
    : ViBase("bad"),
    m_k(k)
    {
    cout << "- Base constructor called: 0x" << hex << this
    << dec << endl << endl;
    };

    const int GetK() const
    {
    return m_k;
    };

    private:

    const int m_k;
    };


    ///////////////////////////////////////////////////////
    class Final
    : public Base
    {
    public:

    Final(const char *s,
    const int k)
    : Base(k),
    ViBase(s)
    {
    cout << "- Final constructor called: 0x" << hex << this
    << dec << endl << endl;
    };

    void Print()
    {
    cout << endl << "Base : S = " << GetS() << endl
    << " K = " << GetK() << endl << endl;
    };

    private:

    int m_a;
    };


    ///////////////////////////////////////////////////////
    int main(int argc, char* argv[])
    {
    Final *x = new Final("ObjectX", 2007);
    if (x == NULL)
    cout << endl << "Out of memory." << endl << endl;

    x->Print();

    cout << "GetS() = " << x->GetS() << endl;
    cout << "GetK() = " << x->GetK() << endl;

    delete x;
    return 0;
    }

    ****************************************************************************************************************************
    ////There are 3 levels here. ViBase <--Base<--Final
    At each level as the object is constructed I print the address of the
    "this" pointer
    I also have a simple function to access some member variable name.
    If I run the program I have no problems at all. The problem arises
    when I use gdb to step into the code.
    Here's what I get. (I have provided a snapshot of the gdb session)

    ****************************************************************************************************************************



    gdb ./a.out
    GNU gdb Red Hat Linux (5.2-2)
    Copyright 2002 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and
    you are
    welcome to change it and/or distribute copies of it under certain
    conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB. Type "show warranty" for
    details.
    This GDB was configured as "i386-redhat-linux"...
    (gdb) b main.cpp:88
    Breakpoint 1 at 0x80489ec: file main.cpp, line 88.
    (gdb) r
    Starting program: /usr2/maheshs/vir_inherit/a.out
    - ViBase constructor called: 0x0x804a4bc s=ObjectX

    - Base constructor called: 0x0x804a4b0

    - Final constructor called: 0x0x804a4b0


    Breakpoint 1, main (argc=1, argv=0xbfffa934) at main.cpp:88
    88 x->Print();
    (gdb) s
    Final::print() (this=0x804a4b0) at main.cpp:71
    71 cout << endl << "Base : S = " << GetS() << endl
    (gdb) p *this
    $1 = {<Base> = {<ViBase> = {m_s = 0x8048f20 "ObjectX"}, _vptr.Base =
    0x8049014, m_k = 2007}, m_a = 0}
    (gdb) n

    Base : S = ObjectX
    K = 2007

    73 };
    (gdb) p x
    No symbol "x" in current context.
    (gdb) l
    68
    69 void Print()
    70 {
    71 cout << endl << "Base : S = " << GetS() << endl
    72 << " K = " << GetK() << endl <<
    endl;
    73 };
    74
    75 private:
    76
    77 int m_a;
    (gdb) n
    main (argc=1, argv=0xbfffa934) at main.cpp:90
    90 cout << "GetS() = " << x->GetS() << endl;
    (gdb) p x
    $2 = (Final *) 0x804a4b0
    (gdb) p x->GetS()
    $3 = 0x8048f20 "ObjectX"
    (gdb) p x->GetK()
    Cannot access memory at address 0x0
    (gdb)


    As you can see above, the pointers printed by the cout statments do
    not correspond to the this pointer which I print. Also in the last
    line, I'm unable to call "x->GetK()"


    Is this a GDB issue?? HAs anyone seen this in the past?
    Is there a patch or some fix available?
    Do let me know
    Thanks,
    Mahesh
     
    Mahesh, May 1, 2007
    #1
    1. Advertising

  2. Mahesh

    Ron Natalie Guest

    Mahesh wrote:

    > Is this a GDB issue?? HAs anyone seen this in the past?
    > Is there a patch or some fix available?
    > Do let me know


    Almost certainly, and completely off topic for this group, try
    gnu.something.

    It looks like gdb doesn't know how to adjust the pointer for the
    virtual base which isn't too surprising, it's a bit tricky.
     
    Ron Natalie, May 1, 2007
    #2
    1. Advertising

  3. Mahesh

    Guest

    On Apr 30, 7:20 pm, Mahesh <> wrote:
    > Hi,
    > I encounted some problems using GDB with classes that use virtual
    > inheritance. To illustrate this issue, I have created a simple test
    > case.
    > Here it goes.
    >
    > #include <iostream>
    > using namespace std;
    >
    > ///////////////////////////////////////////////////////
    > class ViBase
    > {
    > public:
    >
    > ViBase(const char* s)
    > : m_s(s)
    > {
    > cout << "- ViBase constructor called: 0x" << hex << this
    > << " s=" << s << dec << endl << endl;
    > };
    >
    > const char* GetS() const
    > {
    > return m_s;
    > };
    >
    > private:
    >
    > const char* m_s;
    >
    > };
    >
    > ///////////////////////////////////////////////////////
    > class Base
    > : virtual public ViBase
    > {
    > public:
    >
    > Base(const int k)
    > : ViBase("bad"),
    > m_k(k)
    > {
    > cout << "- Base constructor called: 0x" << hex << this
    > << dec << endl << endl;
    > };
    >
    > const int GetK() const
    > {
    > return m_k;
    > };
    >
    > private:
    >
    > const int m_k;
    >
    > };
    >
    > ///////////////////////////////////////////////////////
    > class Final
    > : public Base
    > {
    > public:
    >
    > Final(const char *s,
    > const int k)
    > : Base(k),
    > ViBase(s)
    > {
    > cout << "- Final constructor called: 0x" << hex << this
    > << dec << endl << endl;
    > };
    >
    > void Print()
    > {
    > cout << endl << "Base : S = " << GetS() << endl
    > << " K = " << GetK() << endl << endl;
    > };
    >
    > private:
    >
    > int m_a;
    >
    > };
    >
    > ///////////////////////////////////////////////////////
    > int main(int argc, char* argv[])
    > {
    > Final *x = new Final("ObjectX", 2007);
    > if (x == NULL)
    > cout << endl << "Out of memory." << endl << endl;
    >
    > x->Print();
    >
    > cout << "GetS() = " << x->GetS() << endl;
    > cout << "GetK() = " << x->GetK() << endl;
    >
    > delete x;
    > return 0;
    >
    > }
    >
    > ****************************************************************************************************************************
    > ////There are 3 levels here. ViBase <--Base<--Final
    > At each level as the object is constructed I print the address of the
    > "this" pointer
    > I also have a simple function to access some member variable name.
    > If I run the program I have no problems at all. The problem arises
    > when I use gdb to step into the code.
    > Here's what I get. (I have provided a snapshot of the gdb session)
    >
    > ****************************************************************************************************************************
    >
    > gdb ./a.out
    > GNU gdb Red Hat Linux (5.2-2)
    > Copyright 2002 Free Software Foundation, Inc.
    > GDB is free software, covered by the GNU General Public License, and
    > you are
    > welcome to change it and/or distribute copies of it under certain
    > conditions.
    > Type "show copying" to see the conditions.
    > There is absolutely no warranty for GDB. Type "show warranty" for
    > details.
    > This GDB was configured as "i386-redhat-linux"...
    > (gdb) b main.cpp:88
    > Breakpoint 1 at 0x80489ec: file main.cpp, line 88.
    > (gdb) r
    > Starting program: /usr2/maheshs/vir_inherit/a.out
    > - ViBase constructor called: 0x0x804a4bc s=ObjectX
    >
    > - Base constructor called: 0x0x804a4b0
    >
    > - Final constructor called: 0x0x804a4b0
    >
    > Breakpoint 1, main (argc=1, argv=0xbfffa934) at main.cpp:88
    > 88 x->Print();
    > (gdb) s
    > Final::print() (this=0x804a4b0) at main.cpp:71
    > 71 cout << endl << "Base : S = " << GetS() << endl
    > (gdb) p *this
    > $1 = {<Base> = {<ViBase> = {m_s = 0x8048f20 "ObjectX"}, _vptr.Base =
    > 0x8049014, m_k = 2007}, m_a = 0}
    > (gdb) n
    >
    > Base : S = ObjectX
    > K = 2007
    >
    > 73 };
    > (gdb) p x
    > No symbol "x" in current context.
    > (gdb) l
    > 68
    > 69 void Print()
    > 70 {
    > 71 cout << endl << "Base : S = " << GetS() << endl
    > 72 << " K = " << GetK() << endl <<
    > endl;
    > 73 };
    > 74
    > 75 private:
    > 76
    > 77 int m_a;
    > (gdb) n
    > main (argc=1, argv=0xbfffa934) at main.cpp:90
    > 90 cout << "GetS() = " << x->GetS() << endl;
    > (gdb) p x
    > $2 = (Final *) 0x804a4b0
    > (gdb) p x->GetS()
    > $3 = 0x8048f20 "ObjectX"
    > (gdb) p x->GetK()
    > Cannot access memory at address 0x0
    > (gdb)
    >
    > As you can see above, the pointers printed by the cout statments do
    > not correspond to the this pointer which I print. Also in the last
    > line, I'm unable to call "x->GetK()"
    >
    > Is this a GDB issue?? HAs anyone seen this in the past?
    > Is there a patch or some fix available?
    > Do let me know
    > Thanks,
    > Mahesh


    It looks like you are using an old version of gdb (5.2). Try using a
    newer version (6.5 or 6.6). C++ support has improved quite a bit
    since.
     
    , May 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    826
    Daniel T.
    Feb 4, 2004
  2. cppsks
    Replies:
    0
    Views:
    853
    cppsks
    Oct 27, 2004
  3. Surendra
    Replies:
    0
    Views:
    533
    Surendra
    Mar 23, 2006
  4. seba
    Replies:
    1
    Views:
    1,509
    mlimber
    Mar 22, 2007
  5. Timothy Madden
    Replies:
    1
    Views:
    1,721
    Timothy Madden
    Sep 17, 2009
Loading...

Share This Page