【问题标题】:shared_ptr error while using boost regex使用 boost 正则表达式时出现 shared_ptr 错误
【发布时间】:2018-07-13 17:32:51
【问题描述】:

我在 Codes::blocks 17.12 工作并使用 GNU 安装了 boost 1.66.0。

我是第一次使用 boost 和 boost 正则表达式,所以,我从 this site 复制并粘贴代码并尝试编译它,但失败了。

它显示的错误如下:

需要替换'模板 boost::shared_ptr::shared_ptr(const boost::shared_ptr&, typename boost::detail::sp_enable_if_convertible::type) [其中 Y = const boost::re_detail_106600::cpp_regex_traits_implementation]'|

但是,我什至尝试了各种其他示例,如下所示:

我什至自己写了一些
简而言之,在我尝试过的所有样本中,我都得到了完全相同的错误。

如何消除这个错误以及为什么它首先存在?

【问题讨论】:

    标签: c++ c++11 boost shared-ptr boost-regex


    【解决方案1】:

    我认为当您定义BOOST_REGEX_MATCH_EXTRA 时,实现中存在错误。对我来说,它确实可以编译(同时使用 boost 1.64 和 1.66),但是,它在运行时因 UBSAN 诊断而失败(意味着存在未定义的行为)。诊断是,例如:

    /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426:36: runtime error: member call on misaligned address 0x000000000001 for type 'struct sp_counted_base', which requires 8 byte alignment
    0x000000000001: note: pointer points here
    <memory cannot be printed>
    /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426:36: runtime error: member access within misaligned address 0x000000000001 for type 'struct sp_counted_base', which requires 8 byte alignment
    0x000000000001: note: pointer points here
    <memory cannot be printed>
    ASAN:DEADLYSIGNAL
    =================================================================
    ==24391==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000001 (pc 0x000000406f73 bp 0x7ffe19c54a80 sp 0x7ffe19c54a60 T0)
    ==24391==The signal is caused by a READ memory access.
    ==24391==Hint: address points to the zero page.
        #0 0x406f72 in boost::detail::shared_count::~shared_count() /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426
        #1 0x407800 in boost::shared_ptr<boost::re_detail_106600::named_subexpressions>::~shared_ptr() /home/sehe/custom/boost_1_66_0/boost/smart_ptr/shared_ptr.hpp:341
        #2 0x407a26 in boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::~match_results() /home/sehe/custom/boost_1_66_0/boost/regex/v4/match_results.hpp:110
        #3 0x404c17 in print_captures(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/sehe/Projects/stackoverflow/test.cpp:7
        #4 0x404f81 in main /home/sehe/Projects/stackoverflow/test.cpp:37
        #5 0x7f70051e482f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
        #6 0x404718 in _start (/home/sehe/Projects/stackoverflow/sotest+0x404718)
    
    AddressSanitizer can not provide additional info.
    SUMMARY: AddressSanitizer: SEGV /home/sehe/custom/boost_1_66_0/boost/smart_ptr/detail/shared_count.hpp:426 in boost::detail::shared_count::~shared_count()
    ==24391==ABORTING
    

    事实上,编译器定义BOOST_REGEX_MATCH_EXTRA 只需要额外的捕获信息(第一个链接示例)。如果您不需要它,只需在没有定义的情况下进行编译,所有其他东西都应该可以工作。

    Live On Coliru

    #include <boost/regex.hpp>
    #include <iostream>
    
    void print_captures(const std::string &regx, const std::string &text) {
        boost::regex e(regx);
        boost::smatch what;
        std::cout << "Expression: \"" << regx << "\"\n";
        std::cout << "Text:       \"" << text << "\"\n";
    
        if (boost::regex_match(text, what, e, boost::match_extra)) {
            std::cout << "** Match found **\n   Sub-Expressions:\n";
            for (unsigned i = 0; i < what.size(); ++i)
                std::cout << "      $" << i << " = \"" << what[i] << "\"\n";
    
    #ifdef BOOST_REGEX_MATCH_EXTRA
            std::cout << "   Captures:\n";
            for (unsigned i = 0; i < what.size(); ++i) {
                std::cout << "      $" << i << " = {";
                for (unsigned j = 0; j < what.captures(i).size(); ++j) {
                    if (j)
                        std::cout << ", ";
                    else
                        std::cout << " ";
                    std::cout << "\"" << what.captures(i)[j] << "\"";
                }
                std::cout << " }\n";
            }
    #endif
        } else {
            std::cout << "** No Match found **\n";
        }
    }
    
    int main(int, char *[]) {
        print_captures(R"((([[:lower:]]+)|([[:upper:]]+))+)", "aBBcccDDDDDeeeeeeee");
        print_captures(R"((.*)bar|(.*)bah)", "abcbar");
        print_captures(R"((.*)bar|(.*)bah)", "abcbah");
        print_captures(R"(^(?:(\w+)|(?>\W+))*$)", "now is the time for all good men to come to the aid of the party");
    }
    

    打印

    Expression: "(([[:lower:]]+)|([[:upper:]]+))+"
    Text:       "aBBcccDDDDDeeeeeeee"
    ** Match found **
       Sub-Expressions:
          $0 = "aBBcccDDDDDeeeeeeee"
          $1 = "eeeeeeee"
          $2 = "eeeeeeee"
          $3 = "DDDDD"
    Expression: "(.*)bar|(.*)bah"
    Text:       "abcbar"
    ** Match found **
       Sub-Expressions:
          $0 = "abcbar"
          $1 = "abc"
          $2 = ""
    Expression: "(.*)bar|(.*)bah"
    Text:       "abcbah"
    ** Match found **
       Sub-Expressions:
          $0 = "abcbah"
          $1 = ""
          $2 = "abc"
    Expression: "^(?:(\w+)|(?>\W+))*$"
    Text:       "now is the time for all good men to come to the aid of the party"
    ** Match found **
       Sub-Expressions:
          $0 = "now is the time for all good men to come to the aid of the party"
          $1 = "party"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2011-07-24
      • 2014-10-05
      • 2014-07-01
      相关资源
      最近更新 更多