I get the following runtime error message
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::wave::cpplexer::lexing_exception> >'
what(): boost::wave::lexing_exception
When I am trying to run the following code:
#include <vector>
#include <algorithm>
#include <boost/wave.hpp>
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
std::string strip_comments(std::string const& input)
{
std::string output;
typedef boost::wave::cpplexer::lex_token<> token_type;
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
typedef token_type::position_type position_type;
position_type pos;
lexer_type it = lexer_type(input.begin(), input.end(), pos,
boost::wave::language_support(
boost::wave::support_cpp|boost::wave::support_option_long_long));
lexer_type end = lexer_type();
for (;it != end; ++it)
{
if (*it != boost::wave::T_CCOMMENT
&& *it != boost::wave::T_CPPCOMMENT)
{
output += std::string(it->get_value().begin(), it->get_value().end());
}
}
return output;
}
int main()
{
std::string text="aaa bbb /*cccc*/ ddd // eee";
std::cout<<strip_comments(text)<<std::endl;
return 0;
}
I use gcc compiler:
g++ -std=c++11 test.cpp -o test -lboost_wave -lboost_system
This code is supposed to remove C++ comments from a text specified by // and /* ... */.
Here is gdb backtrack:
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
[New LWP 10573]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./a.out'.
Program terminated with signal SIGABRT, Aborted.
#0 0x00007ff38fe96cc9 in __GI_raise (sig=sig#entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007ff38fe96cc9 in __GI_raise (sig=sig#entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007ff38fe9a0d8 in __GI_abort () at abort.c:89
#2 0x00007ff3904cb78d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ff3904c97f6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ff3904c9841 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ff3904c9a58 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ff390a9e063 in void boost::throw_exception<boost::wave::cpplexer::lexing_exception>(boost::wave::cpplexer::lexing_exception const&)
() from /usr/lib/x86_64-linux-gnu/libboost_wave.so.1.54.0
#7 0x00007ff390aa3a5e in boost::wave::cpplexer::re2clex::lexer<__gnu_cxx::__normal_iterator<char const*, std::string>, boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > >, boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >::report_error(boost::wave::cpplexer::re2clex::Scanner const*, int, char const*, ...) ()
from /usr/lib/x86_64-linux-gnu/libboost_wave.so.1.54.0
#8 0x00007ff390aaf2f0 in boost::wave::cpplexer::re2clex::scan(boost::wave::cpplexer::re2clex::Scanner*) ()
from /usr/lib/x86_64-linux-gnu/libboost_wave.so.1.54.0
#9 0x00007ff390aa4329 in boost::wave::cpplexer::re2clex::lexer<__gnu_cxx::__normal_iterator<char const*, std::string>, boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > >, boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >::get(boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > >&) () from /usr/lib/x86_64-linux-gnu/libboost_wave.so.1.54.0
#10 0x000000000040a3b8 in boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >::get_next<boost::spirit::multi_pass<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::split_functor_input, boost::spirit::iterator_policies::split_std_deque> > > (mp=..., result=...)
at /usr/include/boost/wave/cpplexer/cpp_lex_iterator.hpp:80
#11 0x0000000000409efb in boost::spirit::iterator_policies::split_functor_input::unique<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, true>::advance_input<boost::spirit::multi_pass<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util---Type <return> to continue, or q <return> to quit---
::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::split_functor_input, boost::spirit::iterator_policies::split_std_deque> > > (mp=...)
at /usr/include/boost/spirit/home/support/iterators/detail/split_functor_input_policy.hpp:91
#12 0x000000000040969f in boost::spirit::iterator_policies::multi_pass_unique<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, boost::spirit::iterator_policies::ref_counted::unique, boost::spirit::iterator_policies::no_check::unique, boost::spirit::iterator_policies::split_functor_input::unique<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, true>, boost::spirit::iterator_policies::split_std_deque::unique<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, true, true, true>::advance_input<boost::spirit::multi_pass<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::split_functor_input, boost::spirit::iterator_policies::split_std_deque> > > (mp=...)
at /usr/include/boost/spirit/home/support/iterators/detail/combine_policies.hpp:441
#13 0x0000000000408d7c in boost::spirit::iterator_policies::split_std_deque::unique<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >::increment<boost::spirit::multi_pass<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::split_functor_input, boost::spirit::iterator_policies::split_std_deque> > > (mp=...)
at /usr/include/boost/spirit/home/support/iterators/detail/split_std_deque_policy.hpp:107
#14 0x0000000000408450 in boost::spirit::multi_pass<std::pair<boost::wave::cpplexer::impl::lex_iterator_functor_shim<boost::wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >, boost::wave::cpplexer::lex_input_interface<boost:---Type <return> to continue, or q <return> to quit---
:wave::cpplexer::lex_token<boost::wave::util::file_position<boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> > > > >*>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::split_functor_input, boost::spirit::iterator_policies::split_std_deque> >::operator++ (this=0x7fff7d8f66e0)
at /usr/include/boost/spirit/home/support/iterators/multi_pass.hpp:119
#15 0x000000000040680e in strip_comments (input="aaa bbb /*cccc*/ ddd // eee") at test.cpp:23
#16 0x0000000000406927 in main () at test.cpp:37
Update
Thanks to jpo38 for his comment. I initialized the pos by
position_type pos(0);
And the previous runtime error fixed.
Now, I got a different runtime error:
#0 0x0000000000406ff8 in std::char_traits<char>::length (__s=0x0) at /usr/include/c++/4.9/bits/char_traits.h:263
#1 0x0000000000408504 in boost::wave::util::flex_string<char, std::char_traits<char>, std::allocator<char>, boost::wave::util::CowString<boost::wave::util::AllocatorStringStorage<char, std::allocator<char> >, char*> >::flex_string (this=0x7ffee3d536a0, s=0x0, a=...)
at /usr/include/boost/wave/util/flex_string.hpp:1502
#2 0x000000000040693f in strip_comments (input="aaa bbb /*cccc*/ ddd // eee") at test.cpp:16
#3 0x0000000000406bf0 in main () at test.cpp:37
I catched the general exception and tried to show it by:
std::cout << "Error occurred: " << ex.what() << std::endl;
while it was not successful.
boost love exception-programming. Meaning exceptions are used to raise errors and warnings....I hate that, but you need to accept it if you use those fantastic c++ libraries. Exceptions lead to "crash" if you don't be careful and catch them, like that:
std::string strip_comments(std::string const& input)
{
std::string output;
typedef boost::wave::cpplexer::lex_token<> token_type;
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
typedef token_type::position_type position_type;
boost::wave::util::file_position_type current_position; // for error reporting
try
{
position_type pos;
lexer_type it = lexer_type(input.begin(), input.end(), pos,
boost::wave::language_support(
boost::wave::support_cpp|boost::wave::support_option_long_long));
lexer_type end = lexer_type();
for (;it != end; ++it)
{
current_position = (*it).get_position(); // for error reporting
if (*it != boost::wave::T_CCOMMENT
&& *it != boost::wave::T_CPPCOMMENT)
{
output += std::string(it->get_value().begin(), it->get_value().end());
}
}
}
catch (boost::wave::cpplexer::lexing_exception const& e) {
// some lexing error
std::cerr
<< e.file_name() << "(" << e.line_no() << "): "
<< e.description() << std::endl;
}
catch (std::exception const& e) {
// use last recognized token to retrieve the error position
std::cerr
<< current_position.get_file()
<< "(" << current_position.get_line() << "): "
<< "exception caught: " << e.what()
<< std::endl;
}
catch (...) {
// use last recognized token to retrieve the error position
std::cerr
<< current_position.get_file()
<< "(" << current_position.get_line() << "): "
<< "unexpected exception caught." << std::endl;
}
return output;
}
Then, instead of a "crash", you'll get this nice ans safe message:
(1): warning: generic lexer warning: Unterminated 'C++' style comment
You can see that, by not catching exception, a simple boost warning was transformed in a critical crash at your level ;-)
I'm not familiar with wave library, but adding a \n to the end of your string fixes the issue and then no error is reported anymore (by the way, it was actually just a warning):
Change main function text string to: std::string text="aaa bbb /*cccc*/ ddd // eee\n"; and the program will output aaa bbb ddd.
You can add this \n, but you may also rework the code to try/catch within the loop and then report but ignore warnings.
Note: The fact that boost::wave expects a EOL is probably related to this well known C++ warning (but it is really just a warning): "No newline at end of file" compiler warning
Related
I am trying to connect with vault using libvault library. but, i am getting segmentation fault like below
Trying ..*....
TCP_NODELAY set
Connected to vault..-...net (..*.) port 8200 (#0)
ALPN, offering http/1.1
successfully set certificate verify locations:
CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: none
Thread 5 "batch" received signal SIGSEGV, Segmentation fault.
Code i have written is below
Vault::Token rootToken{"*******"};
Vault::TokenStrategy tokenStrategy{rootToken};
Vault::HttpErrorCallback httpErrorCallback = [&](std::string err) {
std::cout << err << std::endl;
};
Vault::Host host{"vault.*****.***-**.***.***.net"};
Vault:: Port port{"8200"};
//Vault:: ConnectTimeout timeout{10000};
Vault::Config config = Vault::ConfigBuilder().withDebug(true).withTlsEnabled(true).withHost(host).withPort(port).build();
Vault::Client vaultClient{config, tokenStrategy, httpErrorCallback};
Vault::SecretMount mount{"/****/*****/*****"};
Vault::KeyValue kv{vaultClient,mount};
Vault::Path key{"**/***/***"};
std::cout<<"vault check initial<<std::endl;
auto response = kv.read(key);
if (response) {
std::cout<<"vault check"<<std::endl;
std::cout << response.value() << std::endl;
} else {
std::cout << "Unable to read secrets" << std::endl;
}
exception is during reading the kv.read(key)
Below is the stacktrace ...
(gdb) bt
#0 0x0000000000000009 in ?? ()
#1 0x00000000011687be in ossl_connect_step1 (conn=conn#entry=0x7fffe40288a0, sockindex=sockindex#entry=0)
at /3rdparty/curl/lib/vtls/openssl.c:2605
#2 0x000000000116acde in ossl_connect_common (conn=0x7fffe40288a0, sockindex=0, nonblocking=nonblocking#entry=true,
done=0x7ffff488e17d) at /3rdparty/curl/lib/vtls/openssl.c:3457
#3 0x000000000116aed2 in Curl_ossl_connect_nonblocking (conn=<optimized out>, sockindex=<optimized out>,
done=<optimized out>) at /3rdparty/curl/lib/vtls/openssl.c:3543
#4 0x0000000001141f4c in Curl_ssl_connect_nonblocking (conn=conn#entry=0x7fffe40288a0, sockindex=sockindex#entry=0,
done=done#entry=0x7ffff488e17d) at /3rdparty/curl/lib/vtls/vtls.c:275
#5 0x0000000001151cdb in https_connecting (conn=conn#entry=0x7fffe40288a0, done=done#entry=0x7ffff488e17d)
at /3rdparty/curl/lib/http.c:1507
#6 0x000000000115349e in Curl_http_connect (conn=0x7fffe40288a0, done=0x7ffff488e17d)
at /3rdparty/curl/lib/http.c:1436
#7 0x000000000113d346 in Curl_protocol_connect (conn=0x7fffe40288a0, protocol_done=protocol_done#entry=0x7ffff488e17d)
at /3rdparty/curl/lib/url.c:1647
#8 0x000000000112dfd7 in multi_runsingle (multi=multi#entry=0x7fffe4007ed0, now=..., data=data#entry=0x7fffe401e2a0)
at /3rdparty/curl/lib/multi.c:1610
#9 0x000000000112ecd4 in curl_multi_perform (multi=multi#entry=0x7fffe4007ed0,
running_handles=running_handles#entry=0x7ffff488e2c0) at /3rdparty/curl/lib/multi.c:2171
#10 0x00000000011256b2 in easy_transfer (multi=multi#entry=0x7fffe4007ed0)
at /3rdparty/curl/lib/easy.c:686
#11 0x0000000001125809 in easy_perform (data=0x7fffe401e2a0, events=events#entry=false)
at /3rdparty/curl/lib/easy.c:779
--Type <RET> for more, q to quit, c to continue without paging--c
#12 0x0000000001125b08 in curl_easy_perform (data=<optimized out>) at /3rdparty/curl/lib/easy.c:798
#13 0x0000000001115d93 in Vault::HttpClient::CurlWrapper::execute (this=this#entry=0x7ffff488e4a0) at /3rdparty/libvault/include/VaultClient.h:201
#14 0x0000000001114ef6 in Vault::HttpClient::executeRequest(Vault::Tiny<Vault::UrlDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, Vault::Tiny<Vault::TokenDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, Vault::Tiny<Vault::NamespaceDetail, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, std::function<void (void*)> const&, std::function<curl_slist* (curl_slist*)> const&, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)> const&) const (this=this#entry=0x7ffff488e920, url=..., token=..., ns=..., setupCallback=..., curlHeaderCallback=..., errorCallback=...) at /3rdparty/libvault/src/support/HttpClient.cpp:184
#15 0x0000000001114fc5 in Vault::HttpClient::get (this=this#entry=0x7ffff488e920, url=..., token=..., ns=...) at 3rdparty/libvault/src/support/HttpClient.cpp:33
#16 0x0000000001116199 in Vault::HttpConsumer::get (client=..., url=...) at /3rdparty/libvault/src/support/HttpConsumer.cpp:12
#17 0x000000000110f940 in Vault::KeyValue::read (this=this#entry=0x7ffff488e840, path=...) at /3rdparty/libvault/src/engines/KeyValue.cpp:48
With same mount and key , i am able to connect with vault using python library
I trying to write custom data structure: something close to permanent forward autofree list.
Here is my code:
#include <queue>
#include <random>
#include <iostream>
#include <memory>
#include <cassert>
#include <fstream>
template<typename T>
struct RevListNode {
T data;
std::shared_ptr<RevListNode<T>> prev{};
RevListNode(T data = {}, std::shared_ptr<RevListNode<T>> prev = {}) : data(data), prev(prev) {
}
};
template<typename T>
struct RevList {
std::shared_ptr<RevListNode<T>> last{};
int sz = 0;
[[nodiscard]] RevList Chain(T data) const {
RevListNode<T> nw(data, last);
RevList ans{std::make_shared<RevListNode<T>>(nw), sz + 1};
assert(last == nullptr || last.use_count() > 1);
return ans;
}
};
struct State {
RevList<int> taken_list{}; // more size is better
int colors_eliminated = 0; // less is better
bool operator<(const State &other) const {
if (taken_list.sz > other.taken_list.sz) return true;
if (taken_list.sz < other.taken_list.sz) return false;
if (colors_eliminated < other.colors_eliminated) return true;
if (colors_eliminated > other.colors_eliminated) return false;
return false;
}
bool operator>(const State &other) const {
return other < *this;
}
};
using PrqState = std::priority_queue<State, std::vector<State>, std::greater<>>;
const int PURE_N = 9368;
int main(){
std::vector<PrqState> q(PURE_N);
int cnt = 0;
std::ifstream in("log.txt");
std::string s;
q[0].emplace(State{});
State cur = q[0].top();
std::vector < std::array < int, 4 > > ops;
while (in >> s){
if (s == "pop"){
int qind;
in >> qind;
ops.push_back({0, qind, 0, 0});
} else {
int qind, ind, el;
in >> qind >> ind >> el;
ops.push_back({1, qind, ind, el});
}
}
for (int i = 0; i < ops.size();){
//std::cerr << ops[i][0] << std::endl;
assert(ops[i][0] == 0);
int qind = ops[i][1];
State cur = q[qind].top();
q[qind].pop();
int j = i + 1;
for (; j < ops.size() && ops[j][0] == 1; ++j){
//std::cerr << ops[j][0] << std::endl;
qind = ops[j][1];
int ind = ops[j][2];
int el = ops[j][3];
q[qind].emplace(State{cur.taken_list.Chain(ind), el});
}
i = j;
}
}
I don't know when certain part will be free, so I decided use std::shared_ptr (i never dereference it in code).
Log link: https://drive.google.com/file/d/1vtJqNWSSHY-4saQdrSx7p0gC2S715GKd/view?usp=sharing
Some previous versions, when I use debugger it shows me something which similar to this:
https://www.reddit.com/r/cpp_questions/comments/b03z69/copying_an_stdshared_ptr_causes_a_segmentation/
But i have log (which this code emulate) which gives me new-delete-type-mismatch.
Actually I don't know why this happening:
=================================================================
==13141==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x604037f1bc10 in thread T0:
object passed to delete has wrong type:
size of the allocated type: 70368744177704 bytes;
size of the deallocated type: 40 bytes.
#0 0x7f0fcfd11777 in operator delete(void*, unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.6+0xb5777)
#1 0x55a89e608cd3 in __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >::deallocate(std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2>*, unsigned long) /usr/include/c++/11/ext/new_allocator.h:139
#2 0x55a89e6088c9 in std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >::deallocate(std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2>*, unsigned long) /usr/include/c++/11/bits/allocator.h:187
#3 0x55a89e6088c9 in std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> > >::deallocate(std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >&, std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2>*, unsigned long) /usr/include/c++/11/bits/alloc_traits.h:492
#4 0x55a89e608397 in std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> > >::~__allocated_ptr() /usr/include/c++/11/bits/allocated_ptr.h:73
#5 0x55a89e608e92 in std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2>::_M_destroy() /usr/include/c++/11/bits/shared_ptr_base.h:538
#6 0x55a89e6018ad in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() /usr/include/c++/11/bits/shared_ptr_base.h:184
#7 0x55a89e6008c7 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() /usr/include/c++/11/bits/shared_ptr_base.h:702
#8 0x55a89e6000b3 in std::__shared_ptr<RevListNode<int>, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() /usr/include/c++/11/bits/shared_ptr_base.h:1149
#9 0x55a89e6000cf in std::shared_ptr<RevListNode<int> >::~shared_ptr() /usr/include/c++/11/bits/shared_ptr.h:122
#10 0x55a89e600331 in RevList<int>::~RevList() /home/arseny/Projects/RevList/main.cpp:19
#11 0x55a89e60034d in State::~State() /home/arseny/Projects/RevList/main.cpp:32
#12 0x55a89e606926 in void std::destroy_at<State>(State*) /usr/include/c++/11/bits/stl_construct.h:88
#13 0x55a89e607fa9 in void std::_Destroy<State>(State*) /usr/include/c++/11/bits/stl_construct.h:138
#14 0x55a89e607694 in void std::_Destroy_aux<false>::__destroy<State*>(State*, State*) /usr/include/c++/11/bits/stl_construct.h:152
#15 0x55a89e606dd2 in void std::_Destroy<State*>(State*, State*) /usr/include/c++/11/bits/stl_construct.h:185
#16 0x55a89e605758 in void std::_Destroy<State*, State>(State*, State*, std::allocator<State>&) /usr/include/c++/11/bits/alloc_traits.h:746
#17 0x55a89e607e75 in std::vector<State, std::allocator<State> >::~vector() /usr/include/c++/11/bits/stl_vector.h:680
#18 0x55a89e60741d in std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >::~priority_queue() /usr/include/c++/11/bits/stl_queue.h:456
#19 0x55a89e607438 in void std::destroy_at<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > >(std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*) /usr/include/c++/11/bits/stl_construct.h:88
#20 0x55a89e606c06 in void std::_Destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > >(std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*) /usr/include/c++/11/bits/stl_construct.h:138
#21 0x55a89e60544f in void std::_Destroy_aux<false>::__destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*>(std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*, std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*) /usr/include/c++/11/bits/stl_construct.h:152
#22 0x55a89e603460 in void std::_Destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*>(std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*, std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*) /usr/include/c++/11/bits/stl_construct.h:185
#23 0x55a89e601be8 in void std::_Destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*, std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > >(std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*, std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*, std::allocator<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > >&) /usr/include/c++/11/bits/alloc_traits.h:746
#24 0x55a89e6009e9 in std::vector<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >, std::allocator<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > > >::~vector() /usr/include/c++/11/bits/stl_vector.h:680
#25 0x55a89e5ffa1d in main /home/arseny/Projects/RevList/main.cpp:88
#26 0x7f0fceec9bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
#27 0x55a89e5feb29 in _start (/home/arseny/Projects/RevList/main+0x2b29)
0x604037f1bc10 is located 0 bytes inside of 70368744177704-byte region [0x604037f1bc10,0xa04037f1bc38)
allocated by thread T0 here:
#0 0x7f0fcfd10717 in operator new(unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.6+0xb4717)
#1 0x55a89e608c97 in __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long, void const*) /usr/include/c++/11/ext/new_allocator.h:121
#2 0x55a89e6087f0 in std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long) /usr/include/c++/11/bits/allocator.h:173
#3 0x55a89e6087f0 in std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >&, unsigned long) /usr/include/c++/11/bits/alloc_traits.h:460
#4 0x55a89e6082fd in std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> > > std::__allocate_guarded<std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> > >(std::allocator<std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2> >&) /usr/include/c++/11/bits/allocated_ptr.h:97
#5 0x55a89e607a91 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<RevListNode<int>, std::allocator<RevListNode<int> >, RevListNode<int>&>(RevListNode<int>*&, std::_Sp_alloc_shared_tag<std::allocator<RevListNode<int> > >, RevListNode<int>&) /usr/include/c++/11/bits/shared_ptr_base.h:648
#6 0x55a89e6072db in std::__shared_ptr<RevListNode<int>, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<RevListNode<int> >, RevListNode<int>&>(std::_Sp_alloc_shared_tag<std::allocator<RevListNode<int> > >, RevListNode<int>&) /usr/include/c++/11/bits/shared_ptr_base.h:1337
#7 0x55a89e606a08 in std::shared_ptr<RevListNode<int> >::shared_ptr<std::allocator<RevListNode<int> >, RevListNode<int>&>(std::_Sp_alloc_shared_tag<std::allocator<RevListNode<int> > >, RevListNode<int>&) /usr/include/c++/11/bits/shared_ptr.h:409
#8 0x55a89e605068 in std::shared_ptr<RevListNode<int> > std::allocate_shared<RevListNode<int>, std::allocator<RevListNode<int> >, RevListNode<int>&>(std::allocator<RevListNode<int> > const&, RevListNode<int>&) /usr/include/c++/11/bits/shared_ptr.h:861
#9 0x55a89e602eb8 in std::shared_ptr<RevListNode<int> > std::make_shared<RevListNode<int>, RevListNode<int>&>(RevListNode<int>&) /usr/include/c++/11/bits/shared_ptr.h:877
#10 0x55a89e60114f in RevList<int>::Chain(int) const /home/arseny/Projects/RevList/main.cpp:26
#11 0x55a89e5ff85f in main /home/arseny/Projects/RevList/main.cpp:84
#12 0x7f0fceec9bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
SUMMARY: AddressSanitizer: new-delete-type-mismatch (/usr/lib/x86_64-linux-gnu/libasan.so.6+0xb5777) in operator delete(void*, unsigned long)
==13141==HINT: if you don't care about these errors you may set ASAN_OPTIONS=new_delete_type_mismatch=0
==13141==ABORTING
I use this bash script to compile and run it (if I run this script about 5 times I get at least once new-delete-type-mismatch):
#!/bin/bash
g++ -fsanitize=address -g3 -fno-omit-frame-pointer -O0 -ggdb --std=c++20 -o main main.cpp;
time ./main;
Core dump backrace when I ran without sanitizer:
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000055af3a2eae23 in __gnu_cxx::__exchange_and_add_single (__val=-1, __mem=0x55aff507be58) at /usr/include/c++/11/ext/atomicity.h:84
84 _Atomic_word __result = *__mem;
(gdb) bt
#0 0x000055af3a2eae23 in __gnu_cxx::__exchange_and_add_single (__val=-1, __mem=0x55aff507be58) at /usr/include/c++/11/ext/atomicity.h:84
#1 __gnu_cxx::__exchange_and_add_dispatch (__val=-1, __mem=0x55aff507be58) at /usr/include/c++/11/ext/atomicity.h:99
#2 std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x55aff507be50) at /usr/include/c++/11/bits/shared_ptr_base.h:165
#3 0x000055af3a2ea6c1 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x55afb51bbc00, __in_chrg=<optimized out>)
at /usr/include/c++/11/bits/shared_ptr_base.h:702
#4 0x000055af3a2ea374 in std::__shared_ptr<RevListNode<int>, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x55afb51bbbf8, __in_chrg=<optimized out>)
at /usr/include/c++/11/bits/shared_ptr_base.h:1149
#5 0x000055af3a2ea390 in std::shared_ptr<RevListNode<int> >::~shared_ptr (this=0x55afb51bbbf8, __in_chrg=<optimized out>) at /usr/include/c++/11/bits/shared_ptr.h:122
#6 0x000055af3a2eaaea in RevListNode<int>::~RevListNode (this=0x55afb51bbbf0, __in_chrg=<optimized out>) at main.cpp:9
#7 0x000055af3a2ee82d in std::destroy_at<RevListNode<int> > (__location=0x55afb51bbbf0) at /usr/include/c++/11/bits/stl_construct.h:88
#8 0x000055af3a2ee804 in std::allocator_traits<std::allocator<RevListNode<int> > >::destroy<RevListNode<int> > (__a=..., __p=0x55afb51bbbf0)
at /usr/include/c++/11/bits/alloc_traits.h:533
#9 0x000055af3a2ee6d7 in std::_Sp_counted_ptr_inplace<RevListNode<int>, std::allocator<RevListNode<int> >, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x55afb51bbbe0)
at /usr/include/c++/11/bits/shared_ptr_base.h:528
#10 0x000055af3a2eae7f in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x55afb51bbbe0) at /usr/include/c++/11/bits/shared_ptr_base.h:168
#11 0x000055af3a2ea6c1 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x55afb5239a88, __in_chrg=<optimized out>)
at /usr/include/c++/11/bits/shared_ptr_base.h:702
#12 0x000055af3a2ea374 in std::__shared_ptr<RevListNode<int>, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x55afb5239a80, __in_chrg=<optimized out>)
at /usr/include/c++/11/bits/shared_ptr_base.h:1149
#13 0x000055af3a2ea390 in std::shared_ptr<RevListNode<int> >::~shared_ptr (this=0x55afb5239a80, __in_chrg=<optimized out>) at /usr/include/c++/11/bits/shared_ptr.h:122
#14 0x000055af3a2ea44a in RevList<int>::~RevList (this=0x55afb5239a80, __in_chrg=<optimized out>) at main.cpp:19
#15 0x000055af3a2ea466 in State::~State (this=0x55afb5239a80, __in_chrg=<optimized out>) at main.cpp:32
#16 0x000055af3a2ed161 in std::destroy_at<State> (__location=0x55afb5239a80) at /usr/include/c++/11/bits/stl_construct.h:88
#17 0x000055af3a2ede2e in std::_Destroy<State> (__pointer=0x55afb5239a80) at /usr/include/c++/11/bits/stl_construct.h:138
#18 0x000055af3a2ed907 in std::_Destroy_aux<false>::__destroy<State*> (__first=0x55afb5239a80, __last=0x55afb523ff20) at /usr/include/c++/11/bits/stl_construct.h:152
#19 0x000055af3a2ed441 in std::_Destroy<State*> (__first=0x55afb51c2120, __last=0x55afb523ff20) at /usr/include/c++/11/bits/stl_construct.h:185
#20 0x000055af3a2ecac1 in std::_Destroy<State*, State> (__first=0x55afb51c2120, __last=0x55afb523ff20) at /usr/include/c++/11/bits/alloc_traits.h:746
#21 0x000055af3a2edd05 in std::vector<State, std::allocator<State> >::~vector (this=0x7fc03971e830, __in_chrg=<optimized out>) at /usr/include/c++/11/bits/stl_vector.h:680
#22 0x000055af3a2ed7d8 in std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >::~priority_queue (this=0x7fc03971e830, __in_chrg=<optimized out>)
at /usr/include/c++/11/bits/stl_queue.h:456
#23 0x000055af3a2ed7f3 in std::destroy_at<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > > (__location=0x7fc03971e830)
at /usr/include/c++/11/bits/stl_construct.h:88
#24 0x000055af3a2ed304 in std::_Destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > > (__pointer=0x7fc03971e830)
at /usr/include/c++/11/bits/stl_construct.h:138
#25 0x000055af3a2ec926 in std::_Destroy_aux<false>::__destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*> (__first=0x7fc03971e830,
--Type <RET> for more, q to quit, c to continue without paging--
t=0x7fc03975d310) at /usr/include/c++/11/bits/stl_construct.h:152
#26 0x000055af3a2ebb79 in std::_Destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*> (__first=0x7fc039714010, __last=0x7fc03975d310)
at /usr/include/c++/11/bits/stl_construct.h:185
#27 0x000055af3a2eb0d9 in std::_Destroy<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >*, std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > > (__first=0x7fc039714010, __last=0x7fc03975d310) at /usr/include/c++/11/bits/alloc_traits.h:746
#28 0x000055af3a2ea797 in std::vector<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> >, std::allocator<std::priority_queue<State, std::vector<State, std::allocator<State> >, std::greater<void> > > >::~vector (this=0x7ffd49efdb40, __in_chrg=<optimized out>) at /usr/include/c++/11/bits/stl_vector.h:680
#29 0x000055af3a2ea04e in main () at main.cpp:88
My g++ version:
g++ --version
g++ (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
When use std::priority_queue::top() it doesn't check if size() > 0, so std::priority_queue::top() use dealocated memory to use shared_ptr methods on, so cause bad things
Setup environment
1. Install CMake
cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install
2. Install Boost
cd ~
wget https://boostorg.jfrog.io/artifactory/main/release/1.69.0/source/boost_1_69_0.tar.gz
tar xf boost_1_69_0.tar.gz
cd boost_1_69_0
./bootstrap.sh
./b2 ... cxxflags="-std=c++0x -stdlib=libc++" linkflags="-stdlib=libc++" ...
sudo ./b2 toolset=gcc -j4 install
CMakeLists.txt
# Defines AppBase library target.
project(recipe_01)
cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 14)
message(FATAL_ERROR "app requires c++14 or newer")
elseif(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
find_package(Boost 1.60 REQUIRED COMPONENTS regex)
add_executable(main main.cpp)
target_link_libraries(main Boost::regex)
main.cpp
#include <boost/regex.hpp>
#include <iostream>
int main() {
// output the available regex syntaxes
std::cout
<< "Available regex syntaxes:\n"
<< "\t[0] Perl\n"
<< "\t[1] Perl case insensitive\n"
<< "\t[2] POSIX extended\n"
<< "\t[3] POSIX extended case insensitive\n"
<< "\t[4] POSIX basic\n"
<< "\t[5] POSIX basic case insensitive\n"
<< "Choose regex syntax: ";
// correctly set up flags, according to the chosen syntax
boost::regex::flag_type flag;
switch (std::cin.get()) {
case '0': flag = boost::regex::perl;
break;
case '1': flag = boost::regex::perl|boost::regex::icase;
break;
case '2': flag = boost::regex::extended;
break;
case '3': flag = boost::regex::extended|boost::regex::icase;
break;
case '4': flag = boost::regex::basic;
break;
case '5': flag = boost::regex::basic|boost::regex::icase;
break;
default:
std::cout << "Incorrect number of regex syntax. Exiting... \n";
return -1;
}
// Disabling exceptions
flag |= boost::regex::no_except;
//requesting regex patterns in a loop
// Restoring std::cin
std::cin.ignore();
std::cin.clear();
//Getting a String to match: in a loop
std::string regex, str;
do {
std::cout << "Input regex: ";
if (!std::getline(std::cin, regex) || regex.empty()) {
return 0;
}
// Without `boost::regex::no_except`flag this
// constructor may throw
const boost::regex e(regex, flag);
if (e.status()) {
std::cout << "Incorrect regex pattern!\n";
continue;
}
std::cout << "String to match: ";
while (std::getline(std::cin, str) && !str.empty()) {
bool matched = boost::regex_match(str, e);
std::cout << (matched ? "MATCH\n" : "DOES NOT MATCH\n");
std::cout << "String to match: ";
} // end of `while (std::getline(std::cin, str))`
std::cout << '\n';
// Restoring std::cin
std::cin.ignore();
std::cin.clear();
} while (1);
return 0;
} // int main()
To build
mkdir build
cd build
cmake ..
cmake --build .
And I get this error:
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `bool boost::regex_match<__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> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 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> > > > > >&, boost::basic_regex<char, boost::regex_traits<char,
boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
main.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x8f): undefined reference to `boost::re_detail_106900::perl_matcher<__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> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
CMakeFiles/main.dir/main.cpp.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
main.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*,
unsigned int)'
CMakeFiles/main.dir/main.cpp.o: In function `boost::re_detail_106900::perl_matcher<__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> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>
> >, 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> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
main.cpp:(.text._ZN5boost16re_detail_10690012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_[_ZN5boost16re_detail_10690012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_]+0x121): undefined reference to `boost::re_detail_106900::perl_matcher<__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> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:84: recipe for target 'bin/main' failed
make[2]: *** [bin/main] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I can not link to boost:regex with main function
Could you please tell me any solution for this issue?
I find some quote like
The boost 1.61 (and higher) are compiled with newer compilers, which use the _GLIBCXX_USE_CXX11_ABI=1 .. heh ABI. Meaning that for -std=c++11 std::basic_string... becomes std::__cxx11::basic_string... (and likewise for std::list; see the gcc Dual ABI documentation).
Since your linker error claims there is a missing symbol that contains std::string (which is short for std::basic_string<char,std::char_traits,std::allocator>), you are either compiling with a compiler that is too old and uses ABI 0, or you are even compiling explicitly with -D_GLIBCXX_USE_CXX11_ABI=0.
But how can I fix in my code?
Change the line
target_link_libraries(main Boost::regex)
to
target_link_libraries(main boost_regex)
And depending on your distro you may need to install some Boost devel packages.
My program hangs hard (nigh freezes my whole computer), and I'm trying to figure out where it's happening. I've gone through some gdb tutorials and other things—and followed them pretty well. Whenever I see gdb output, though, it's somewhat clean and readable (that is, its output seems to at least relate to the file which it is debugging).
Here's a small chunk of my gdb's output:
23 std::map<std::string, int> Item::getStats() { return enhancements_; };
(gdb) step
Python Exception <class 'ValueError'> Cannot find type const std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> > >::_Rep_type:
map (__x=std::map with 2 elements, this=0x7fffffffd0c0)
at src/item.cpp:23
23 std::map<std::string, int> Item::getStats() { return enhancements_; };
(gdb) step
_Rb_tree (__x=..., this=0x7fffffffd0c0) at src/item.cpp:23
23 std::map<std::string, int> Item::getStats() { return enhancements_; };
(gdb) step
_Rb_tree_impl (__a=<optimized out>, __comp=..., this=0x7fffffffd0c0)
at /usr/include/c++/4.9/bits/stl_tree.h:474
474 _M_header(), _M_node_count(0)
(gdb) step
475 { _M_initialize(); }
(gdb) step
_M_initialize (this=0x7fffffffd0c0)
at /usr/include/c++/4.9/bits/stl_tree.h:484
484 this->_M_header._M_left = &this->_M_header;
(gdb)
485 this->_M_header._M_right = &this->_M_header;
(gdb) step
_Rb_tree (__x=..., this=0x7fffffffd0c0)
at /usr/include/c++/4.9/bits/stl_tree.h:674
674 if (__x._M_root() != 0)
(gdb) step
_M_root (this=0x7fffffffd038)
at /usr/include/c++/4.9/bits/stl_tree.h:498
498 { return this->_M_impl._M_header._M_parent; }
Is there something up with my gdb or is this how it's generally supposed to read? I'm not seeing anything from the other header files or code outside of main.cpp. Only three of the above lines seem to relate to my code at all.
Edit:
I'm using G++ 4.9.2 and GDB 7.7.1 on Ubuntu 14.04 w/ Python 2.7.6
The output of 'where' is:
(gdb) where
#0 _int_malloc (av=0x7ffff7840760 <main_arena>, bytes=48) at malloc.c:3775
#1 0x00007ffff75047b0 in __GI___libc_malloc (bytes=48) at malloc.c:2891
#2 0x00007ffff7aebf18 in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00000000004016f7 in allocate (__n=1, this=0x7fffffffd0c0) at /usr/include/c++/4.9/ext/new_allocator.h:104
#4 allocate (__n=1, __a=...) at /usr/include/c++/4.9/bits/alloc_traits.h:357
#5 _M_get_node (this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_tree.h:385
#6 _M_create_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> const&> (this=0x7fffffffd0c0)
at /usr/include/c++/4.9/bits/stl_tree.h:417
#7 _M_clone_node (__x=0xa7b82fa0, this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_tree.h:445
#8 std::_Rb_tree<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::less<std::string>, std::allocator<std::pair<std::string const, int> > >::_M_copy (this=this#entry=0x7fffffffd0c0, __x=0xa7b82fa0, __p=0xa7b82fa0,
__p#entry=0x7fffffffd0c8) at /usr/include/c++/4.9/bits/stl_tree.h:1219
#9 0x0000000000401cc5 in _Rb_tree (__x=..., this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_tree.h:676
Python Exception <class 'ValueError'> Cannot find type const std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> > >::_Rep_type:
#10 map (__x=std::map with 2 elements, this=0x7fffffffd0c0) at /usr/include/c++/4.9/bits/stl_map.h:183
#11 Item::getStats (this=0x7fffffffd020) at src/item.cpp:23
#12 0x00000000004011a5 in main () at src/main.cpp:26
I can walk up and down the stack, backtrace, set breaks, and things like this—but all the output is similar in its format. I was expecting to see something a little cleaner, with more relevance to the code that I'm debugging. Something like what's in this tutorial:
Breakpoint 1, LinkedList<int>::remove (this=0x40160,
item_to_remove=#0xffbef014) at main.cc:52
52 Node<T> *marker = head_;
(gdb) step
53 Node<T> *temp = 0; // temp points to one behind as we iterate
(gdb)
55 while (marker != 0) {
(gdb)
56 if (marker->value() == item_to_remove) {
(gdb)
Node<int>::value (this=0x401b0) at main.cc:30
30 const T& value () const { return value_; }
(gdb)
LinkedList<int>::remove (this=0x40160, item_to_remove=#0xffbef014)
at main.cc:75
75 marker = 0; // reset the marker
(gdb)
76 temp = marker;
(gdb)
77 marker = marker->next();
(gdb)
Node<int>::next (this=0x0) at main.cc:28
28 Node<T>* next () const { return next_; }
(gdb)
Program received signal SIGSEGV, Segmentation fault.
Node<int>::next (this=0x0) at main.cc:28
28 Node<T>* next () const { return next_; }
(gdb)
Mine seems unintelligible compared to that.
I have no idea why your program freezes your whole computer, but the problem is obvious right here:
75 marker = 0; // reset the marker
76 temp = marker;
77 marker = marker->next();
If you ignore line 76, and just read the code, it appears to be clearly wrong. Perhaps you meant to write:
marker = marker->next();
im using gzip_decompressor() from the boost library but i'm getting a strange Segmentation fault on free().
stringstream gzMessage;
stringstream rawMessage;
gzMessage << (*iter)->message;
filtering_streambuf<input> gzFilter;
gzFilter.push(gzip_decompressor());
gzFilter.push(gzMessage);
boost::iostreams::copy(gzFilter, rawMessage);
message = rawMessage.str();
cout << "ungzipped: " << message << endl;
The line in question is boost::iostreams::copy(gzFilter, rawMessage);
if i comment this one out everything works fine.
the backtrace:
(gdb) bt
#0 0x00007ffff640fd29 in free () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x0000000000453f8c in boost::detail::sp_counted_impl_p<boost::iostreams::symmetric_filter<boost::iostreams::detail::zlib_decompressor_impl<std::allocator<char> >, std::allocator<char> >::impl>::dispose()
()
#2 0x0000000000427f49 in boost::detail::shared_count::~shared_count() ()
#3 0x00000000004598ae in boost::iostreams::stream_buffer<boost::iostreams::basic_gzip_decompressor<std::allocator<char> >, std::char_traits<char>, std::allocator<char>, boost::iostreams::input>::~stream_buffer() ()
#4 0x0000000000459c39 in boost::iostreams::detail::chain_base<boost::iostreams::chain<boost::iostreams::input, char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, std::allocator<char>, boost::iostreams::input>::chain_impl::~chain_impl() ()
#5 0x0000000000459d12 in boost::detail::sp_counted_impl_p<boost::iostreams::detail::chain_base<boost::iostreams::chain<boost::iostreams::input, char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, std::allocator<char>, boost::iostreams::input>::chain_impl>::dispose() ()
#6 0x0000000000427f49 in boost::detail::shared_count::~shared_count() ()
any hints ?