Boost logo

Boost Users :

Subject: [Boost-users] Problem with signals2 and shared_ptr
From: Glenn Macgregor (gmacgregor_at_[hidden])
Date: 2010-05-26 11:33:47


Hi All,

I am using boost 1.41.0 on Windows with Visual Studio 2008 and am seeing what seems to be an issue with signals2 and shared_ptr. The problem is that the shared_ptr does not decrement is reference count after I disconnect the signals2::connection. This is probably best illustrated with some code.

  ===== Test.h=====
#ifndef _CLASS1_H
#define _CLASS1_H

#include <boost/signals2/signal.hpp>
#include <boost/smart_ptr.hpp>

class Callee : public boost::enable_shared_from_this<Callee> {
public:
    Callee() :
      mInt(0)
    {};
    virtual ~Callee() {};

    void func1() { mInt++; };

private:
    int mInt;
};

class Caller {
public:
    typedef boost::signals2::signal<void (boost::shared_ptr<Callee>)> OnRun;

public:
    Caller() :
      mOnRun()
    {};
    ~Caller() {};

    void run(boost::shared_ptr<Callee> sp)
    {
      boost::signals2::connection conn = mOnRun.connect(boost::bind(&Callee::func1, sp));
      _sleep(1000);
      conn.disconnect();
    };

private:
    OnRun mOnRun;
};
#endif
===== End Test.h=====

======main.cpp======

int
main(int argc, char **argv)
{
                Caller caller;
                {
                                boost::shared_ptr<Callee> sp(new Callee);
                                caller.run(sp);
                }
}
======End main.cpp======

Running this in the debugger I would expect to hit a breakpoint in the Callee class before the Caller class, this is not the case I destruct Caller before Callee. Having dug in a bit I see the use_count of the shared pointer go to 2 while in the caller.run which I would expect. The strange thing is the use_count does not decrement after the disconnect or exiting the caller.run function.

Is there a way to force the connection/signal to release that shared_ptr?

Thanks

 Glenn



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