Boost logo

Boost Users :

Subject: Re: [Boost-users] Problems with boost::coroutines with lambda captures
From: Tamas.Ruszkai_at_[hidden]
Date: 2015-09-17 02:49:32


Thanks Mikhail, it solved the problem! Such a trivial error... :-(
Actually it was the pull_type, not the push_type, but I guess this is also what you have meant.

In the updated version I store also the function object inside the generator.

        template <typename TOutput>
        using GeneratorBlockYield = typename boost::coroutines::asymmetric_coroutine<TOutput>::push_type;

        template<typename TOutput>
        class GeneratorBlockEnumerator : public IEnumerator<TOutput>
        {
        public:
                explicit GeneratorBlockEnumerator(std::function<void(GeneratorBlockYield<TOutput>&)> generator)
                        : _firstMoveNextCall(true)
                        , _generatorFunctor(generator)
                        , _generatorPuller(_generatorFunctor)
                {
                }

                ~GeneratorBlockEnumerator() override = default;

                bool MoveNext() override
                {
                        auto generatorNotFinished = static_cast<bool>(_generatorPuller);
                        if (!generatorNotFinished)
                                return false;

                        if (_firstMoveNextCall)
                        {
                                _firstMoveNextCall = false;
                        }
                        else
                        {
                                _generatorPuller();
                        }

                        generatorNotFinished = static_cast<bool>(_generatorPuller);
                        return generatorNotFinished;
                }

                TOutput Current() const override
                {
                        return _generatorPuller.get();
                }

        private:
                bool _firstMoveNextCall;
                std::function<void(GeneratorBlockYield<TOutput>&)> _generatorFunctor;
                typename boost::coroutines::asymmetric_coroutine<TOutput>::pull_type _generatorPuller;
        };




From:        Mikhail Strelnikov <mikhail.strelnikov@gmail.com>
To:        boost-users@lists.boost.org
Date:        17.09.2015 04:24
Subject:        [Boost-users] Problems with boost::coroutines with lambda captures
Sent by:        "Boost-users" <boost-users-bounces@lists.boost.org>




> Sumamry: somehow the captured smart pointer gets destroyed+corrupted
> when I yield from the coroutine, but the local copy survives. When I use

GeneratorBlockEnumerator gets parameter (std::function) by value (temporary function gets created before constructor and destroyed right after). Then this temporary function passed by reference to push_type.
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


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