0

For some reason boost::regex overloads my application and it freezes without an error, but it compiles fine. For instance this code fails flatly. What am I doing wrong? I updated to boost 1.47 to see if it was a DLL error, but it still doesn't work. Can I get an example program to test out the boost::regex?

static const boost::regex expression("^[0-9]+");
std::string str = "123a1";
std::cout << boost::regex_search(str.c_str(), expression);
Speed
  • 1,424
  • 1
  • 16
  • 24
  • What does this code have to do with threading? Are you using threads? – Nicol Bolas Aug 02 '11 at 09:15
  • Yes I am using threads, but the whole thread function is too large to post. – Speed Aug 02 '11 at 09:17
  • For some reason the program breaks even if I comment out everything in main and just execute the boost::regex sample... Could the boost includes clash between each other? I have included boost::regex, boost::algorithm::string, boost::algorithm::string::regex, boost::thread, boost::mutex – Speed Aug 02 '11 at 09:28
  • 1
    I put your code in an empty main() and it worked fine. I use static linking with Boost, so it may be a DLL issue. – Ferruccio Aug 02 '11 at 14:40

2 Answers2

0

The first thing to do is to see if your version of Boost supports threading. Compiling and running something like the following should tell you that:

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

int
main()
{
#ifdef BOOST_HAS_THREADS
    std::cout << "Boost has threads" << std::endl;
#else
    std::cout << "Boost doesn't support threads" << std::endl;
#endif
    return 0;
}

The second thing is to verify that all of the requirements are met. You've just posted the actual lines, not the context in which they are executed. If the first line is in namespace scope, you should be OK (unless you've started threading in constructors to static objects, before entering main: don't do that). If the first line has block scope (i.e. is in a function), then you are only OK if the first call to this function occurs before threading starts. (From what I understand, with g++, you should be OK even if the first line has block scope, but I'm not sure.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • boost supports threads, I use two threads in the application and the code is a bit too large to post. The regex gets executed inside the thread function to check if some string matches the regex. – Speed Aug 02 '11 at 09:15
  • I added a scheme of my program. – Speed Aug 02 '11 at 09:24
  • So the regular expression itself has block scope. Move it out of the function, and see if that changes anything. (In general, local static variables don't work in multithreaded environments. With most compilers, at least.) – James Kanze Aug 02 '11 at 09:29
  • I moved it to main, out of any threads and it still breaks the program. How I would get it to execute outside a function I don't know. – Speed Aug 02 '11 at 09:34
  • For some reason the above regex code breaks even if I try to use it in a new project with just the boost::regex included. Is my syntax incorrect? – Speed Aug 02 '11 at 09:36
  • If you want the regular expression initialized before entering `main`, put it outside of any function, either as a static member of the class, or a static member in namespace scope. (Also, you don't need, or want, to call `.c_str()` on a `string` before passing it into `boost::regex_search`.) – James Kanze Aug 02 '11 at 10:42
  • I just want to make it work inside a function, something like `std::cout << boost::regex_search(str, boost::regex("^[0-9]+"));` ANY boost::regex query doesn't work and just freezes up the exe, even in a new empty project. I suspect my boost::regex.dll may be broken. – Speed Aug 02 '11 at 11:07
  • Still doesn't work and I updated to 1.47... Can you give me a full example program for me to try and compile? – Speed Aug 02 '11 at 12:06
  • The following works on my Windows machine: `static boost::regex const matchDigits( "^[0-9]+" ); int main() { std::string line; while ( std::getline( std::cin, line ) ) { if ( regex_search( line, matchDigits ) ) { std::cout << line << std::endl; } } return 0; }` – James Kanze Aug 02 '11 at 13:37
  • The code does not work for me. Could you send me your boost::regex DLL so I can test if my DLL malfunctions? – Speed Aug 02 '11 at 14:24
  • I'm not allowed to send DLL's from here, but if the above code doesn't work (with the necessary includes added, of course), then either your boost isn't installed correctly, or you're doing something wrong on the command line (compiling against one version, and linking against another would be an obvious possibility). – James Kanze Aug 02 '11 at 14:34
0

After some work I deleted the boost installation from BoostPro and compiled boost myself and now it works. The problem was that BoostPro did not install all of the DLL's and I thought when it asked me for a missing DLL that BoostPro named them wrong (boost_regex-vc100-mt-1_47.dll instead of boost_regex-vc100-mt-gd-1_47.dll). After getting the correct DLL everything works fine. Thank you for your help troubleshooting this!

Speed
  • 1,424
  • 1
  • 16
  • 24