Sam Lee
Sam Lee

Reputation: 7199

boost::regex segfaults when using capture

I get a seg fault for the simple program below. It seems to be related to the destructor match_results.

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

using namespace std;

int main(int argc, char *argv)
{
    boost::regex re;
    boost::cmatch matches;

    boost::regex_match("abc", matches, re.assign("(a)bc"));

    return 0;
}

edit: I am using boost 1.39

Upvotes: 4

Views: 1800

Answers (4)

Homer6
Homer6

Reputation: 15159

I was having the same problem. I tried the solution posted by Drew Dormann, but it didn't work. Then I discovered that I was actually linking against 1.40, but for some reason the headers were for 1.37. Once I downloaded the correct headers (1.40), it stopped segfaulting.

I noticed it when I had compiled with the debugging symbols -g and run a dbg backtrace..

Hope that helps...

Upvotes: 0

Alex Ott
Alex Ott

Reputation: 87174

You are using temporary variable from which you want to obtain matches. I think, that your problem will resolved, if instead "abc" you will use following:

string a("abc);
regex_match(a, matches, re.assign("(a)bc"));

Upvotes: 0

Drew Dormann
Drew Dormann

Reputation: 63775

boost::regex is one of the few components of boost that doesn't exist solely in header files...there is a library module.

It is likely that the library you are using was built with different settings than your application.

Edit: Found an example scenario with this known boost bug, where boost must be built with the same -malign-double flag as your application.

This is one of several possible scenarios where your boost library will not have binary compatibility with your application.

Upvotes: 4

stefanB
stefanB

Reputation: 79810

Which version of boost are you using?

I compiled the above example with boost 1.36 and I don't get any seg faults.

If you have multiple boost libraries make sure that at runtime you're picking up the correct version.

Boost regex requires to be compiled against library -lboost_regex-gcc_whatever-is-your- version

In my case:

g++ -c -Wall -I /include/boost-1_36_0 -o main.o main.cpp
g++ -Wall -I /include/boost-1_36_0 -L/lib/boost-1_36_0 -lboost_regex-gcc33-mt main.o -o x

to execute:

LD_LIBRARY_PATH=/lib/boost-1_36_0 ./x

You would point to the location of boost include/libs on your system, note the version of gcc and m(ulti) t(hreaded) in library name - it depends on what you have compiled, just look in your boost lib directory and pick one version of regex library from there.

Upvotes: 0

Related Questions