|
Boost : |
Subject: Re: [boost] [Review.Coroutine] Lifetime of coroutine parameters
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2012-09-11 02:46:57
> 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.
your exchange addresses between main() and coroutine-fn (lifetime()).
you could use it also a second time etc. as long as the object does not go out of scope.
> Once the first call to yield return, the memory these
> parameter *3* can point to trash, as the example shows.
sorry - that is not an issue of boost.coroutine itself.
Who would store pointers/references to objects and expects that those objects can still be accessed over the pointers/references after those objects were destroyed. But that is the case in your example.
Your example in issue *3* is equivalent to storing an reference as member in some class and access the member after the original object was gone out of scope.
struct X
{ void g() {} };
struct Y
{
X & x;
Y( X & x_) : x( x_) {}
void f() { x.g(); }
};
Y u()
{
X x;
Y y( x);
y.f(); // OK
}
void main()
{
Y y( u() );
y.f(); // segfault
}
Oliver
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk