1

这是我用来测试正则表达式的代码:

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

int main(int argc, char** argv)
{
    auto isMatch = [](const std::string& text, const std::string& pattern)->bool
    {
        return boost::regex_search(text,
            boost::regex(pattern, boost::regex::icase | boost::regex::nosubs)
        );
    };

    std::cout << std::boolalpha << isMatch("Hello, world!",
        R"(((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;)))") << std::endl;
    return 0;
}

代码编译得很好,但是当我运行它时,有时我会遇到断言失败,因为我可以安全地假设它是一个不应为 null 的 null shared_ptr。它似乎是随机发生的,如下所示:

[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
regextest: /usr/include/boost/smart_ptr/shared_ptr.hpp:646: typename boost::detail::sp_dereference<T>::type boost::shared_ptr<T>::operator*() const [with T = boost::regex_traits_wrapper<boost::regex_traits<char> >; typename boost::detail::sp_dereference<T>::type = boost::regex_traits_wrapper<boost::regex_traits<char> >&]: Assertion `px != 0' failed.
Aborted (core dumped)
[jacktrueborn@inpost regextest]$ ./regextest
false
[jacktrueborn@inpost regextest]$

这就是我安装boost的方式:

./bootstrap.sh --prefix=/usr
sudo ./b2 install

我知道这个库过去一直存在这个问题,但我一直无法找到令人满意的解决方法;我想知道一个是否已经浮出水面,我只是还没有看到它。但是,鉴于错误的片状性质,我不确定我是否也遇到了同样的问题。std::regex只是在运行时抛出std::regex_errors 所以我不能使用它。

(我知道 isMatch 在这种情况下不需要是 lambda 甚至是真正的函数,但在我的非人为代码中,这样做是有原因的。)

4

1 回答 1

0

The issue was, as pointed out, two different versions of Boost being installed. However, the steps for how to fix the problem were not addressed in the suggested question. Here is what I had to do:

  1. Remove the system packaged Boost library (I don't know if this step was actually required because of the following two steps I had to perform):

    sudo yum remove boost-devel

  2. Remove all the leftover Boost libs:

    sudo find /usr -name 'libboost_*' -delete

  3. Remove all the leftover Boost includes including the Boost folder itself:

    sudo rm -rf /usr/include/boost

  4. Reinstall Boost 1.53 (from above):

    ./bootstrap.sh --prefix=/usr

    sudo ./b2 install

In my case, I also had to update LD_LIBRARY_PATH to point to /usr/lib as the variable didn't even exist before.

At any rate, the code works now. Every time. No runtime errors at all.

于 2013-06-27T01:02:00.080 回答