Boost logo

Boost Users :

Subject: Re: [Boost-users] [Spirit] simplified struct.cpp coredumps
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-04-03 23:42:18


On Sat, Apr 3, 2010 at 9:40 PM, philip tucker <phhht_at_[hidden]> wrote:
> Hello,
>
> I have a Spirit program that fails with a segmentation fault and I can't see
> why.  I hope you can help.
>
> I'm starting from a simplified version of struct.cpp, known to work.  I want
> to abstract the identifier grammar into its own struct and use that instead
> of the inline grammar for an identifier.  The problem code is attached.
>
> What have I done wrong?
>
> Thanks as always for your patient, expert, and helpful comments.
>
> P.S.
>
> I'm running boost_1_42_0 with
>
> gcc -v:
> Using built-in specs.
> Target: i686-apple-darwin9
> Configured with: /var/tmp/gcc_42/gcc_42-5574~1/src/configure
> --disable-checking --enable-werror --prefix=/usr --mandir=/usr/share/man
> --enable-languages=c,objc,c++,obj-c++
> --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib
> --build=i686-apple-darwin9 --with-gxx-include-dir=/usr/include/c++/4.0.0
> --host=i686-apple-darwin9 --target=i686-apple-darwin9
> Thread model: posix
> gcc version 4.2.1 (Apple Inc. build 5574)
>
> on
>
> Model Name: iMac
> Model Identifier:   iMac9,1
> Processor Name: Intel Core 2 Duo
> Processor Speed:    2.66 GHz
> Number Of Processors:   1
> Total Number Of Cores:  2
> L2 Cache:   6 MB
> Memory: 2 GB
> Bus Speed:  1.07 GHz
> Boot ROM Version:   IM91.008D.B08
> SMC Version (system):   1.44f0
>
> P.S.S
>
> If you want the coredump, let me know.

I cannot run it right now, not at home, but first of all, you can
change the id grammar to this (will run and compile faster):

    template <typename Iterator>
    struct id_grammar : qi::grammar<Iterator, std::string()>
    {
        id_grammar() : id_grammar::base_type(start)
        {
            using qi::int_;
            using qi::raw;
            using ascii::alpha;
            using ascii::alnum;
            start %= raw[('_' | alpha) >> *(alnum | '_' | '-')];
        }
        qi::rule<Iterator, std::string()> start;
    };

The thing that is probably causing your failure is that you id grammar
instance is being destroyed when your main grammar constructor exits
on this line:
            client::id_grammar<Iterator> id;

You need to change your whole main grammar to:

    template <typename Iterator>
    struct employee_grammar : qi::grammar<Iterator,employee(),ascii::space_type>
    {
        employee_grammar() : employee_grammar::base_type(start)
        {
            using qi::int_;
            using qi::double_;
            using qi::lexeme;
            using ascii::alpha;
            using ascii::alnum;

            start %=
                    int_
>> id
>> id
>> double_
                ;
        }

        /*******************************************************/
        qi::rule<Iterator, std::string(), ascii::space_type> id;
        /*******************************************************/
        qi::rule<Iterator, employee(), ascii::space_type> start;
        client::id_grammar<Iterator> id;
    };

Does that work?

Remember, if you are creating a grammar/rule, it needs to stick around
for the whole parse, just like you stuff your rule into start (a class
data member), you need your grammar a class data member too that you
use in your rule. (There are ways to work around that with smart
pointers and other nasty things, but it is completely unnecessary and
slower, ignore them, do it the above way.)


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