Boost logo

Boost :

Subject: [boost] [Review.Coroutine] Lifetime of coroutine parameters
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-09-10 17:28:06


Hi,

I have some concerns about the lifetime of the coroutine parameters.

Surprisingly (well not really), the following example doesn't fails at
*1*. This is because the coroutine have access to its coroutine
parameters *2*, which has a sens only for the first time the coroutine
is called. Once the first call to yield return, the memory these
parameter *3* can point to trash, as the example shows.

typedef int TT;
typedef boost::coro::coroutine< void(TT&) > coro_TT;

void lifetime( coro_TT::self_t & self, TT& i ) // *2*
{
     i=1;
     TT& j = self.yield();
     std::cout << i << j << std::endl;

     i=2; // *3* UNSAFE access to i
     self.yield();
}
void test_lifetime()
{
     coro_TT c( boost::bind( lifetime, _1, _2) );
     {
         int k = 0;
         c( k );
         BOOST_CHECK(k = 1);

     }
     {
         int k = 0;
         int i = 0;
         c( i );
         BOOST_CHECK(k = 2); // *1*
         std::cout << i << k << std::endl;
     }
}

This could hide errors that are difficult to catch. In addition to merit
to be mentioned in the documentation as a limitation of the current
library interface I really think that it merits to see if this can be
solved.

Best,
Vicente


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