shared_ptr error while using boost regex

时间:2018-02-03 09:24:23

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

I am working in Codes::blocks 17.12 and installed boost 1.66.0 using GNU.

I am using boost and boost regex for first time, so, to start with i copied and pasted code from this site and tried compiling it, but failed.

the error which it is showing is following:

required by substitution of 'template boost::shared_ptr::shared_ptr(const boost::shared_ptr&, typename boost::detail::sp_enable_if_convertible::type) [with Y = const boost::re_detail_106600::cpp_regex_traits_implementation]'|

however, i even tried various other examples, listed below:

i even wrote some of them by myself
In short, in all the samples i had tried, i got exactly the same error.

How do remove this error and why in the first place it is there ?

1 个答案:

答案 0 :(得分:0)

我认为在定义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"