Re: [Boost-bugs] [Boost C++ Libraries] #12220: Memory leak in future::then()

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #12220: Memory leak in future::then()
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-08-18 19:23:37


#12220: Memory leak in future::then()
-------------------------------+-------------------------------------
  Reporter: valentin.milea@… | Owner: viboes
      Type: Bugs | Status: assigned
 Milestone: To Be Determined | Component: thread
   Version: Boost 1.61.0 | Severity: Regression
Resolution: | Keywords: memory leak future then
-------------------------------+-------------------------------------

Comment (by BloodyRookie):

 "static_shared_from_this(this) is used as parameter of the run function
 and the parameter is released when the function ends, isn't it?"

 Where is the parameter released?

 Suppose I have code like this

 {{{
 {
    auto f = boost::make_ready_future<int>(41).then([](boost::future<int>)
 { return 42; });
    f.get(); // wait for the future to complete
 }
 }}}

 Then at some point

 {{{
 this->thr_ = thread(&future_async_shared_state::run,
 static_shared_from_this(this), boost::forward<Fp>(f));
 }}}

 Is called. The thread ctor calls

 {{{
 thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1)))
 }}}

 a1 is

 boost::shared_ptr<boost::detail::future_async_continuation_shared_state<boost::future<int>,int,int
 <lambda>(boost::future<int>)>>

 The result of boost::bind is stored in the thread_data member (actually
 the pointer thread_data_ptr keeps it alive)

 After the future completes the situation has not changed, nothing is
 released imo.
 In the debugger I can see the following:

 http://imgur.com/a/7u5ii

 There are 2 strong references on the shared state, one is from the
 variable f but the other one is from the creation of the thread which is
 attached to the future. The dtor of the future is never called.

 If I comment out the BOOST_THREAD_FUTURE_BLOCKING flag definition then at
 the same break point there is only 1 strong reference and the dtor of the
 future is called.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12220#comment:6>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC