使用boost正则表达式时出现shared_ptr错误

shared_ptr error while using boost regex

本文关键字:shared ptr 错误 boost 正则表达式 使用      更新时间:2023-10-16

我正在代码::块17.12中工作,并使用GNU安装了boost 1.66.0。

我是第一次使用boost和boost regex,所以,首先,我复制并粘贴了这个网站的代码,并尝试编译它,但失败了。

它显示的错误如下:

需要替换"template"boost::shared_ptr::shared_ptboost::detail::sp_enable_if_filterable::type)[其中Y=constboost::re_detail_106600::cpp_rege_traits_iimplementation]'|

然而,我甚至尝试了其他各种例子,如下所示:

  • http://www.boost.org/doc/libs/1_66_0/libs/regex/doc/html/boost_regex/partial_matches.html

  • http://www.boost.org/doc/libs/1_66_0/libs/regex/doc/html/boost_regex/ref/regex_match.html

  • http://www.boost.org/doc/libs/1_66_0/libs/regex/doc/html/boost_regex/ref/regex_search.html

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

如何删除这个错误,为什么会出现这个错误?

我认为在定义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仅用于额外的捕获信息(第一个链接的样本)。如果您不需要,只需在没有定义的情况下进行编译,所有其他东西都应该可以工作。

在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"