From 5b19ad6f4d7b2e2d93b8bb2d09c4c4c40b50eb89 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Mon, 4 May 2015 22:59:28 -0400 Subject: [PATCH] Fixed threading issue in Onigura Regex library --- src/openalpr/postprocess/regexrule.cpp | 15 +++++++++++---- src/openalpr/postprocess/regexrule.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/openalpr/postprocess/regexrule.cpp b/src/openalpr/postprocess/regexrule.cpp index 2e3cad4..9493e23 100644 --- a/src/openalpr/postprocess/regexrule.cpp +++ b/src/openalpr/postprocess/regexrule.cpp @@ -20,14 +20,17 @@ #include "regexrule.h" using namespace std; - + +tthread::mutex regexrule_mutex_m; + namespace alpr { RegexRule::RegexRule(string region, string pattern) - { + { this->original = pattern; this->region = region; + this->regex = ""; this->valid = false; string::iterator end_it = utf8::find_invalid(pattern.begin(), pattern.end()); @@ -90,12 +93,16 @@ namespace alpr numchars++; } + // Onigurama is not thread safe when compiling regex. Using a mutex to ensure that + // we don't crash + regexrule_mutex_m.lock(); UChar* cstr_pattern = (UChar* )this->regex.c_str(); OnigErrorInfo einfo; - - //cout << "Pattern: " << cstr_pattern << endl; + int r = onig_new(&onig_regex, cstr_pattern, cstr_pattern + strlen((char* )cstr_pattern), ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo); + + regexrule_mutex_m.unlock(); if (r != ONIG_NORMAL) { //char s[ONIG_MAX_ERROR_MESSAGE_LEN]; diff --git a/src/openalpr/postprocess/regexrule.h b/src/openalpr/postprocess/regexrule.h index 89a2e55..3542899 100644 --- a/src/openalpr/postprocess/regexrule.h +++ b/src/openalpr/postprocess/regexrule.h @@ -26,6 +26,7 @@ #include #include "support/regex/oniguruma.h" #include "support/utf8.h" +#include "support/tinythread.h" namespace alpr {