简体   繁体   English

提升正则表达式运行时错误

[英]Boost regex runtime error

I'm trying to use some code that I wrote on another computer that splits a string into tokens. 我正在尝试使用我在另一台将字符串拆分为标记的计算机上编写的代码。 This code compiles fine. 这段代码编译得很好。 The code also works as intended on some other computers. 该代码也可以在其他一些计算机上运行。 I've managed to reduce the code down to the following: 我已设法将代码减少到以下内容:

#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;
}

This compiles without errors or warnings using: g++ -Wall test.cpp -lboost_regex but when used at runtime providing the input test 100 the program fails. 这使用以下命令编译时没有错误或警告: g++ -Wall test.cpp -lboost_regex但是在运行时使用时提供输入test 100程序失败。

action: 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 > >]: Assertion `px != 0' failed. 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'失败。

Aborted 中止

I'm completely lost as to what is going on here. 我完全迷失了这里发生的事情。 Is this a bug in in my code or in the library? 这是我的代码或库中的错误吗? Any advice for debugging this is greatly appreciated! 任何有关调试的建议都非常感谢!

That is not a bug . 那不是错误 It's conflict of boost header files. 它是boost头文件的冲突。

It may be because of wrong file inclusion, or because of wrong library inclusion (regex module is one of the few boost modules that needs compilation). 这可能是因为文件包含错误,或者包含错误的库(正则表达式模块是少数需要编译的boost模块之一)。

You should make sure you are using the correct files by using -l and -I switches, eg: 您应该使用-l和-I开关确保使用正确的文件,例如:

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

It happens when you compile with headers from a boost version and execute with another boost version. 当您使用boost版本的头文件进行编译并使用另一个boost版本执行时,会发生这种情况。 You should check which library of boost is installed for execution, and which you use for compilation. 您应该检查安装哪个boost库以便执行,以及用于编译的库。

Run this in gdb, or some similar program, set a breakpoint at the start of these sections, then step through until you find the offending line. 在gdb或类似程序中运行此命令,在这些部分的开头设置断点,然后逐步执行直到找到有问题的行。

The error you are getting looks like an invalid pointer is being passed to the boost library somewhere. 你得到的错误看起来像一个无效的指针正被传递到某个地方的boost库。

Since you're not using shared_ptr in your code, and I can see no other stuff that looks wrong AND it works on other machines, I'd say it's probably a bug in Boost.Regex. 因为你没有在你的代码中使用shared_ptr ,而且我看不到其他看起来错误的东西并且它在其他机器上工作,我会说它可能是Boost.Regex中的一个错误。

I bet the other machines have other versions of boost installed? 我打赌其他机器安装了其他版本的boost?

If I had to guess I'd try changing the std::cout << *iter++ << std::endl; 如果我不得不猜测我会尝试更改std::cout << *iter++ << std::endl; line first. 先行。 -> std::cout << *iter << std::endl; ++iter; - > std::cout << *iter << std::endl; ++iter; std::cout << *iter << std::endl; ++iter; .

And yeah, run it in a debugger like Swiss suggested, and see where the assertion is triggered. 是的,在瑞士建议的调试器中运行它,看看断言被触发的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM