Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost regex runtime error

Tags:

c++

regex

boost

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.

action: test 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.

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!

like image 292
shuttle87 Avatar asked Apr 04 '11 23:04

shuttle87


2 Answers

That is not a bug. It's conflict of boost header files.

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).

You should make sure you are using the correct files by using -l and -I switches, e.g.:

   g++ -W -Wall main.cpp $(LDFLAGS) -lboost_regex -I/data1/PROJECT_SEARCH/libsrc/boost_1_46_1
like image 91
hwajong Avatar answered Oct 07 '22 06:10

hwajong


It happens when you compile with headers from a boost version and execute with another boost version. You should check which library of boost is installed for execution, and which you use for compilation.

like image 26
Nikko Avatar answered Oct 07 '22 05:10

Nikko