Boost::spirit program crashes
Here my stripped version still crashes with segfault:
----<snio>----
#include
On 3/26/2010 3:35 PM, Lars Rohwedder wrote:
Here my stripped version still crashes with segfault:
----<snio>---- #include
#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
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.
I thought I bound the temporary to a const reference, so the temporary lives as long as the reference lives, like in this example: std::string& s = std::string("Foo") + "Bar";
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.
That's why I want to use references, to avoid copying rules around. I solved the problem by writing // unchanged: struct Configurable { const char* name; const rule<>& rule; }; // make an explicit object here, not a bound temporary: const rule<> myRule = +(__)>>(...); // bound the reference to the copy: const Configurable cfg = {"Name", myRule }; and it seems to work. :-) Lars R.
On 3/26/2010 9:43 PM, Lars Rohwedder wrote:
I solved the problem by writing
// unchanged: struct Configurable { const char* name; const rule<>& rule; };
// make an explicit object here, not a bound temporary: const rule<> myRule = +(__)>>(...);
// bound the reference to the copy: const Configurable cfg = {"Name", myRule };
and it seems to work. :-)
Yes, that will work. 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
participants (2)
-
Joel de Guzman
-
Lars Rohwedder