Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::spirit program crashes
From: Joel de Guzman (joel_at_[hidden])
Date: 2010-03-26 05:01:57


On 3/26/2010 3:35 PM, Lars Rohwedder wrote:
> Here my stripped version still crashes with segfault:
>
> ----<snio>----
> #include<boost/spirit.hpp>
> #include<iostream>
>
> using namespace boost::spirit;
>
> struct Configurable
> {
> const char* name; // name of the configurable module
> const rule<>& parser; // parser for config items, normally
> // a couple of OR-connected terms.
> };
>
> rule<> __ = ch_p(' '); // separator char. was a bit more complex
> // in the original code
>
> std::string conn, port, ident_name, ident_pw;
>
[...]
>
> // a simplicistic parser that still crashes:
> const Configurable cfg = { "Hardtest", +( __ ) };
>
>
> int main(int argc, char** argv)
> {
> // original config string:
> // "connect foo.bar:123 identity root fF2.oby!sy" or the like.
> parse("hard ", cfg.parser); //<--- CRASHES!
>
> // this works:
> // parse("hard ", +(__) );
> };
> ----<snap>----
>
[...]
> Perhaps somebody knows an answer except "use spirit 2!" :-)

You have a dangling reference to a rule here:

    const rule<>& parser;

when you do this:

    const Configurable cfg = { "Hardtest", +( __ ) };

+( __ ) creates a temporary rule and you assign the address of that
to cfg.parser. Remove the reference:

    const rule<> parser;

Keep in mind though that "classic" spirit rules are not copyable.
So don't go and copy your cfg around.

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net
http://www.facebook.com/djowel
Meet me at BoostCon
http://www.boostcon.com/home
http://www.facebook.com/boostcon

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