问题描述
我正在与我的灵奇解析器频繁的段错误。
I'm running into frequent segfaults with my Spirit Qi parser.
消磨时间来调试问题之后(我发现不可能神交踪迹),我决定把修剪下来到最小的例子。谁能告诉我我做错了,如果有什么?
After spending days to debug the issue (I found the stacktraces impossible to grok) I decided to trim it down to a minimal example. Can anyone tell what I'm doing wrong, if anything?
保存code作为bug.cpp,用 G ++ -Wall -o错误bug.cpp编译
,你应该是好去。
Save code as bug.cpp, compile with g++ -Wall -o bug bug.cpp
and you should be good to go.
//#define BOOST_SPIRIT_DEBUG_PRINT_SOME 80
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/version.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
namespace /*anon*/
{
using namespace boost::spirit::qi;
template <typename Iterator, typename
Skipper> struct bug_demo :
public grammar<Iterator, Skipper>
{
bug_demo() :
grammar<Iterator, Skipper>(story, "bug"),
story(the),
the("the")
{
// BOOST_SPIRIT_DEBUG_NODE(story);
// BOOST_SPIRIT_DEBUG_NODE(the);
}
rule<Iterator, Skipper> story, the;
};
template <typename It>
bool do_parse(It begin, It end)
{
bug_demo<It, space_type> grammar;
return phrase_parse(begin, end, grammar, space);
}
}
int main()
{
std::cout << "Spirit version: " << std::hex << SPIRIT_VERSION << std::endl;
try
{
std::string contents = "the lazy cow";
if (do_parse(contents.begin(), contents.end()))
return 0;
} catch (std::exception e)
{
std::cerr << "exception: " << e.what() << std::endl;
}
return 255;
}
我已经测试了这个
I've tested this with
- 先按g ++ 4.4,4.5,4.6和
- 增压版本1.42(Ubuntu的猫鼬)和1.46.1.1(整洁的)
的输出是
sehe@meerkat:/tmp$ ./bug
Spirit version: 2020
Segmentation fault
或者,用升压1.46.1将报告精神版本:2042
Or, with boost 1.46.1 it will report Spirit version: 2042
推荐答案
更改初始化顺序,你在你的答案建议只是隐藏的问题。实际的问题是,规则&LT;&GT;
的有适当的C ++复制语义。您可以通过重写你的gramar初始化为解决这个问题:
Changing the initialization order as you suggested in your answer just hides the problem. The actual problem is, that rule<>
's have proper C++ copy semantics. You can fix this by rewriting your gramar initialization as:
bug_demo() :
grammar<Iterator, Skipper>(story, "bug"),
story(the.alias()),
the("the")
{}
有关的理由和更详细的说明,请参阅 href=\"http://boost-spirit.com/home/articles/doc-addendum/faq/#aliases\">。
For a rationale and a more detailed explanation, see here.
这篇关于分段故障琐碎的精神语法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!