Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examine boost shared_ptr with gdb

Tags:

Following is my source code:

#include <iostream>
#include <boost/shared_ptr.hpp>

class MyClass
{
    public:
        MyClass()
        {
            i=10;
        }
    private:
        int i;
};


int main(int argc, const char *argv[])
{
    boost::shared_ptr <MyClass> obj(new MyClass());
    return 0;
}

I want to examine obj in gdb, and view the value of member variable i.

This is what I get with normal print:

29          boost::shared_ptr <MyClass> obj(new MyClass());
(gdb) n
30          return 0;
(gdb) p obj
$1 = {px = 0x602010, pn = {pi_ = 0x602030}}

I tried the tip mentioned in this link , but does not work.

(gdb) call (obj.get())->print()
Cannot evaluate function -- may be inlined

Is there any other way? gdb version is 7.0.1.

like image 238
m.divya.mohan Avatar asked Jul 23 '13 08:07

m.divya.mohan


1 Answers

Got it.!

(gdb) set print pretty
(gdb) p obj
$5 = {
  px = 0x602010,
  pn = {
    pi_ = 0x602030
  }
}
(gdb) p obj.px
$6 = (MyClass *) 0x602010



(gdb) p *(obj.px)
$7 = {
  i = 10
}
like image 159
m.divya.mohan Avatar answered Dec 25 '22 00:12

m.divya.mohan