【问题标题】:Boost regex runtime error提升正则表达式运行时错误
【发布时间】:2011-07-29 13:50:22
【问题描述】:

我正在尝试使用我在另一台计算机上编写的一些代码,将字符串拆分为标记。这段代码编译得很好。该代码也可以在其他一些计算机上按预期工作。我设法将代码减少到以下内容:

#include <string>
#include <boost/regex.hpp>

typedef std::vector<std::string> token_t ;

token_t generate_tokens(std::string raw_input){ 
//this function breaks a input string into tokens. So test 100 goes to 2 tokens "test" and "100".

    boost::regex re_splitter("\\s+"); //this uses the regex \s+ to find whitespace. The + finds one or more whitespace characters.

    boost::sregex_token_iterator iter(raw_input.begin(), raw_input.end(), re_splitter, -1);
    //this breaks the string using the regex re_splitter to split into tokens when that is found. 
    boost::sregex_token_iterator j; //This is actually in the Boost examples, j is the equivalent of end. Yes this did also seem weird to me at first...

    token_t token_vector;
    unsigned int count = 0;
    while(iter != j)
    {
        token_vector.push_back(*iter);
        std::cout << *iter++ << std::endl;
        ++count;
    }
    return token_vector;
}

int main(){
    std::string in;
    int amount = -1;

    std::cout << "action: ";
    std::getline(std::cin, in);

    boost::regex EXPR("^test \\d*(\\.\\d{1,2})?$");
    bool format_matches = boost::regex_match(in, EXPR);

    token_t tokens = generate_tokens(in);

    if(format_matches){
        amount = atoi(tokens.at(1).c_str());
    }
    std::cout << "amount: " << amount << "\n";
    return 0;
}

编译时没有错误或警告使用:g++ -Wall test.cpp -lboost_regex 但是在运行时提供输入test 100 时,程序会失败。

动作:测试 100

a.out: /usr/local/include/boost/smart_ptr/shared_ptr.hpp:412: typename boost::detail::shared_ptr_traits::reference boost::shared_ptr::operator*() const [with T = boost::regex_traits_wrapper > >]: 断言 `px != 0' 失败。

中止

我完全不知道这里发生了什么。这是我的代码或库中的错误吗?非常感谢任何有关调试的建议!

【问题讨论】:

  • 你使用的是什么版本的 boost?该代码和输入在 MSVC 2010 SP1 和 Boost 1.46.1 上运行良好

标签: c++ regex boost


【解决方案1】:

在 gdb 或其他类似程序中运行它,在这些部分的开头设置一个断点,然后逐步执行,直到找到有问题的行。

你得到的错误看起来像是一个无效的指针被传递到某个地方的 boost 库。

【讨论】:

    【解决方案2】:

    由于您没有在代码中使用shared_ptr,而且我看不到其他看起来有问题的东西,而且它在其他机器上也能正常工作,我会说这可能是 Boost.Regex 中的一个错误。

    我敢打赌其他机器安装了其他版本的 boost?

    如果我不得不猜测,我会先尝试更改 std::cout &lt;&lt; *iter++ &lt;&lt; std::endl; 行。 -> std::cout &lt;&lt; *iter &lt;&lt; std::endl; ++iter;.

    是的,在 Swiss 建议的调试器中运行它,然后查看触发断言的位置。

    【讨论】:

    • 我进行了建议的更改并遇到了完全相同的问题。
    • 好吧,这只是一个疯狂的猜测。正如 Swiss 和我已经建议的那样:启动 gdb(或任何其他调试器)并查看触发 assert() 的位置。然后从那里去。如果它是 Boost.Regex 中的错误,您应该检查它是否仍然存在于最新版本(我认为是 1.46)中,如果是,请提交错误报告。
    【解决方案3】:

    这不是错误。是boost头文件冲突。

    这可能是因为错误的文件包含,或者是因为错误的库包含(regex 模块是少数需要编译的 boost 模块之一)。

    您应该通过使用 -l 和 -I 开关来确保使用正确的文件,例如:

       g++ -W -Wall main.cpp $(LDFLAGS) -lboost_regex -I/data1/PROJECT_SEARCH/libsrc/boost_1_46_1
    

    【讨论】:

      【解决方案4】:

      当您使用来自 boost 版本的头文件进行编译并使用另一个 boost 版本执行时会发生这种情况。您应该检查安装了哪个 boost 库来执行,以及您使用哪个库来编译。

      【讨论】:

        猜你喜欢
        • 2017-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-24
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多