Boost logo

Boost :

From: Jonathan Biggar (jon_at_[hidden])
Date: 2006-12-13 20:14:27


Theodore Witkamp wrote:
> Hi, I would like to get some feedback on some changes that I would
> like to make to boost::function for my own use only and I figured that
> this would be the best place for it.
>
> I basically want to detect if there is a NULL "this" bound to a member
> function. I want to do this so that i can prevent dereferencing a NULL
> pointer when calling a member function. I hope the following code
> example will clarify.
> ///////////////////////////////////////////////////////////////////////
> // example code:
> ///////////////////////////////////////////////////////////////////////
> struct X
> {
> void f()
> {
> assert(this != NULL);
> }
> };
>
> my_smart_ptr<X> sp_x = new X();
> my_weak_ptr<X> wp_x = sp_x;
> boost::function0<void> _f = bind(&X::f,wp_x);
> assert(!_f.empty());
> sp_x.reset(); // wp_x == NULL
>
> if(!_f.empty())
> {
> _f(); // This will fail
> }
> ////////////////////////////////////////////////////////////////////////

I don't see how that code can work anyway, since you can't use a
weak_ptr to directly invoke X::f.

-- 
Jonathan Biggar
jon_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk