The virtual function getMyVal() of MyClass returns a MyVal object
as value, and MyVal object size is small, and MyVal object has copy
constructor. 

When I use "p myClass->getMyVal()", the gdb calls the
copy ctor of MyVal badly, and copy ctor overwrite the vptr of the
myClass. 

When I remove the copy ctor of MyVal, or I add some dummy
members to MyVal, the gdb will work correctly. 

Tested in Ubuntu 11.10,
64 bit. (On Ubuntu 11.10, 32 bit, work correctly) 

See attached files.


Zoltan Bojthe. 
  
class MyVal
{
    int val;
    // when added next line, the gdb print work correctly when dimension >= 4
    // int dummy[4];
  public:
    MyVal() { val = 42; }
    MyVal(int _val) { val = _val; }
    // when remove the next copy ctor, the gdb print work correctly
    MyVal(const MyVal& other) { val = other.val; }
};

class MyClassBase
{
  public:
    virtual MyVal getMyVal() = 0;
};

class MyClass : public MyClassBase
{
    MyVal myVal;
  public:
    virtual MyVal getMyVal() { return myVal; }
};

int main(int argc, const char **argv)
{
    MyClassBase *myClass = new MyClass();
    MyVal m;
    m = myClass->getMyVal();
    m = myClass->getMyVal();
}
g++ -g a.cc
start
n
n
p *myClass
# {_vptr.MyClassBase = <valid_vptr_address>}
p myClass->getMyVal()
# {val = <address of myClass>}
p *myClass
# {_vptr.MyClassBase = 0x0}
p myClass->getMyVal()
#Cannot access memory at address 0x0
gdb a.out <gdb.in >gdb.out 2>&1
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/zoli/Projects/proba/gdb/a.out...done.
(gdb) Temporary breakpoint 1 at 0x4006a4: file a.cc, line 29.
Starting program: /home/zoli/Projects/proba/gdb/a.out 

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe188) at a.cc:29
29          MyClassBase *myClass = new MyClass();
(gdb) 30            MyVal m;
(gdb) 31            m = myClass->getMyVal();
(gdb) $1 = {_vptr.MyClassBase = 0x400920}
(gdb) (gdb) $2 = {val = 42}
(gdb) (gdb) $3 = {_vptr.MyClassBase = 0x400920}
(gdb) (gdb) $4 = {val = 42}
(gdb) (gdb) quit
A debugging session is active.

        Inferior 1 [process 2440] will be killed.

Quit anyway? (y or n) [answered Y; input not from terminal]
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/zoli/Projects/proba/gdb/a.out...done.
(gdb) Temporary breakpoint 1 at 0x4006a4: file a.cc, line 29.
Starting program: /home/zoli/Projects/proba/gdb/a.out 

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe188) at a.cc:29
29          MyClassBase *myClass = new MyClass();
(gdb) 30            MyVal m;
(gdb) 31            m = myClass->getMyVal();
(gdb) $1 = {_vptr.MyClassBase = 0x4008e0}
(gdb) (gdb) $2 = {val = 6299664}
(gdb) (gdb) $3 = {_vptr.MyClassBase = 0x20fe1}
(gdb) (gdb) Cannot access memory at address 0x20fe1
(gdb) (gdb) quit
A debugging session is active.

        Inferior 1 [process 3144] will be killed.

Quit anyway? (y or n) [answered Y; input not from terminal]
_______________________________________________
bug-gdb mailing list
bug-gdb@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-gdb

Reply via email to