[ 
https://issues.apache.org/jira/browse/MESOS-6484?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15614689#comment-15614689
 ] 

Alexander Rojas commented on MESOS-6484:
----------------------------------------

So, here is how the leak works: An object {{Future<T>}} is a wrapper for 
{{std::shared_ptr<Future<T>::Data> data}}. This part of the code 
{{lambda::bind(&internal::expired<T>, f, latch, promise, *this)}} creates a 
copy of {{this}}, where {{data}} is pointed to by {{this}} and the new copy of 
the future. This copy is stored in the callable object created by 
{{lambda::bind()}} to be executed in the future. When we execute the 
{{onAny(lambda::bind(&internal::after<T>, latch, promise, timer, 
lambda::_1))}}, a callable object with a copy of {{timer}} is stored in the 
{{onAnyCallbacks}} which is in itself stored in {{data}}, which ends up being 
{{data}} having indirectly a reference counted pointer to itself, therefore it 
will never be destroyed.

> Memory leak in `Future<T>::after()`
> -----------------------------------
>
>                 Key: MESOS-6484
>                 URL: https://issues.apache.org/jira/browse/MESOS-6484
>             Project: Mesos
>          Issue Type: Bug
>          Components: libprocess
>    Affects Versions: 1.1.0
>            Reporter: Alexander Rojas
>              Labels: libprocess, mesosphere
>
> The problem arises when one tries to associate an {{after()}} call to copied 
> futures. The following test case is enough to reproduce the issue:
> {code}
> TEST(FutureTest, After3)
> {
>   auto policy = std::make_shared<int>(0);
>   {
>     auto generator = []() {
>       return Future<Nothing>();
>     };
>     Future<Nothing> future = generator()
>       .after(Milliseconds(1),
>         [policy](const Future<Nothing>&) {
>            return Nothing();
>         });
>     AWAIT_READY(future);
>   }
>   EXPECT_EQ(1, policy.use_count());
> }
> {code}
> In the test, one would expect that there is only one active reference to 
> {{policy}}, therefore the expectation {{EXPECT_EQ(1, policy.use_count())}}. 
> However, if after is triggered more than once, each extra call adds one 
> undeleted reference to {{policy}}.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to