Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2008-07-12 09:46:47


It looks to me like the problem is the binds using shared_ptrs as
instance pointers. Boost.Bind takes a copy of the shared_ptr while
constructing the bind object (which is used to construct the funcion
object). You then pass the function object to the GUI/event handler,
which then contains a shared_ptr to the event handler/GUI respectively
- as a shared_ptr is an ownership link, that this means that the GUI
'owns' the event handler and vice versa.

An option would be to perform those binds using raw pointers - for
example, use m_gui.get() rather than just m_gui. That would prevent
the 'ownership' links being created. The smart pointer library *does*
offer weak_ptr as a non-owning smart pointer, but I don't know if
Boost.Bind works with that - worth investigating, though?

HTH
Stuart Dootson

On Sat, Jul 12, 2008 at 12:14 PM, Simon Pickles
<sipickles_at_[hidden]> wrote:
> Hello,
>
> I am using boost::functions to provide a simple event system, but have come
> across a problem.
>
> I have two classes that need to communicate with each other, CGUI and
> CEventHandler. Their instances are both shred_ptrs owned by a third class,
> CCore.
>
<snip>
>
> As you can see, CCore binds a public member function of m_gui and passes it
> to the eventhandler, where it is stored and used to send events.
>
> The event handler returns its own member function, bound to this, which
> CCore duly passes to m_gui.
>
> Now both classes hold objects with refs to each other, and the program does
> a big memory dump when it is ended, on win32 with MSVC2005. I envisage this
> is because the shared_ptrs holding the instances of CGUI and CEvenHandler
> are not destructing at program close.
>
> Commenting out the line:
> m_guiTx = f;
>
> eliminates the issue.
>
> What is a better way to achieve this?
>
> Thanks
>
> Simon


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