Search code examples
c++boostboost-spirit-qiboost-phoenix

Destructor gets called when using phoenix construct


I'm working on a Boost Spirit Qi project that uses phoenix::construct to create an object that has a pointer to another object. I noticed that using phoenix::construct calls the destructor at some point (I'm guessing at the end of the function). Why does the destructor get called? How do I get around this?

Note: this isn't my actual code, I made a toy example so it wouldn't be so long

Objects

class Bar
{
public:
    Bar(int y)
    {
        this->x = y;
    }

    ~Bar()
    {
    }

    int x;
};

class Foo
{
public:

    Foo()
    {
    }

    Foo(Bar* bar) : _bar(bar)
    {
    }

    ~Foo()
    {
        delete this->_bar;
    }

    void DoStuff()
    {
        std::cout << this->_bar->x << std::endl;
    }

private:
    Bar *_bar;
};

Grammar

template <typename Iterator>
struct TestGrammar : qi::grammar < Iterator, Foo(), ascii::space_type >
{
    TestGrammar() : TestGrammar::base_type(foo)
    {
        foo = bar[qi::_val = phoenix::construct<Foo>(qi::_1)];
        bar = qi::double_[qi::_val = phoenix::new_<Bar>(qi::_1)];
    }

    qi::rule < Iterator, Foo(), ascii::space_type > foo;
    qi::rule < Iterator, Bar*(), ascii::space_type> bar;
};

Calling Code

std::getline(std::cin, string);

iter = string.begin();
end = string.end();

bool result = qi::phrase_parse(iter, end, grammar, space, f);

if (result)
{
    State s;
    f.DoStuff();
}
else
{
    std::cout << "No Match!" << std::endl;
}

Solution

  • The destructor runs on the attribute of the foo rule; that is after it has been copied into the referenced attribute.

    Since your Foo class violates Rule-Of-Three this causes problems. Foo should not be copyable (or implement deep copy).

    Marking as such:

    class Foo : public boost::noncopyable {
    

    will reveal that the grammar copies the Foo:

    /home/sehe/custom/boost_1_57_0/boost/proto/transform/default.hpp|154 col 9| error: use of deleted function ‘Foo& Foo::operator=(const Foo&)’
    

    In short:

    1. Write hygienic classes with Rule-Of-Zero or Rule-Of-{Three|Five}
    2. Don't do dynamic allocation in Spirit grammars. It'll give you grief.
    3. Don't do semantic actions (Boost Spirit: "Semantic actions are evil"?)

    Here's a quick&dirty fixed version by implementing Rule-Of-Three:

    Live On Coliru

    struct Foo {
        Foo(Bar *bar = 0) : _bar(bar) {}
        Foo(Foo const& o) : _bar(new Bar(*o._bar)) {}
        Foo& operator=(Foo const& o) { 
            Foo tmp;
            tmp._bar = new Bar(*o._bar);
            std::swap(tmp._bar, _bar);
            return *this;
        }
    
        ~Foo() { delete _bar; }
    
        void DoStuff() { std::cout << _bar->x << std::endl; }
    
      private:
        Bar *_bar;
    };
    

    And here's a slightly less quick&dirty using Rule Of Zero:

    Live On Coliru

    struct Foo {
        using PBar = boost::shared_ptr<Bar>;
        Foo(PBar bar = {}) : _bar(bar) {}
    
        void DoStuff() { std::cout << _bar->x << std::endl; }
    
    private:
        PBar _bar;
    };
    

    Full Code

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <boost/make_shared.hpp>
    
    namespace qi      = boost::spirit::qi;
    namespace phoenix = boost::phoenix;
    namespace ascii   = boost::spirit::ascii;
    
    struct Bar {
        Bar(int y) { this->x = y; }
        ~Bar() {}
    
        int x;
    };
    
    struct Foo {
        using PBar = boost::shared_ptr<Bar>;
        Foo(PBar bar = {}) : _bar(bar) {}
    
        void DoStuff() { std::cout << _bar->x << std::endl; }
    
      private:
        PBar _bar;
    };
    
    template <typename Iterator>
    struct TestGrammar : qi::grammar<Iterator, Foo(), ascii::space_type>
    {
        TestGrammar() : TestGrammar::base_type(foo)
        {
            using namespace qi;
            foo = bar     [ _val = phoenix::construct<Foo::PBar>(_1) ] ;
            bar = double_ [ _val = phoenix::new_<Bar>(_1)      ] ;
        }
    
        qi::rule<Iterator, Foo(), ascii::space_type> foo;
        qi::rule<Iterator, Bar*(), ascii::space_type> bar;
    };
    
    int main() {
        std::string input;
        std::getline(std::cin, input);
    
        using It = std::string::const_iterator;
        It iter = input.begin();
        It end = input.end();
    
        TestGrammar<It> grammar;
        Foo f;
        bool result = qi::phrase_parse(iter, end, grammar, ascii::space, f);
    
        if (result) {
            //State s;
            f.DoStuff();
        } else {
            std::cout << "No Match!" << std::endl;
        }
    }