4

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

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Sam Lee
  • 7,049
  • 10
  • 42
  • 47

4 Answers4

4

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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

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

Homer6
  • 15,034
  • 11
  • 61
  • 81
0

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.

stefanB
  • 77,323
  • 27
  • 116
  • 141
0

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"));
Alex Ott
  • 80,552
  • 8
  • 87
  • 132