Boost logo

Boost Users :

Subject: [Boost-users] [bind] and [function] : question on binding member function
From: Moritz (mokoman_at_[hidden])
Date: 2009-09-08 07:25:10


Hi,

can someone explain to me why the following code crashes at the third
call to "a.call_func" when "str" is dereferenced? Should'nt it crash
when "this" is dereferenced?

Thanks in advance.

typedef boost::function<void (void)> Callback;

class A
{
   public:
      void set_cb_func( Callback cb ) { callback = cb; }
      void call_func() { callback(); }
      Callback callback;
};

class B
{
   public:
    B() : str(new std::string) {}
    ~B() { delete str; }

    void do_sth() { std::cerr << &(*this) << " " << *str << std::endl;
*str += "string"; };
    std::string * str;
};

int main()
{
    A a;
    {
        B b;
        a.set_cb_func(boost::bind(&B::do_sth, &b));
        a.call_func();
        a.call_func();
    }
    a.call_func();
    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net