私が実行しているC++コードに問題があります。基本的には、ほとんどの入力でうまく動作しますが、特定の入力ではメイン関数が復帰した後にセグメンテーションが行われます。これは困惑しています。私は、スタックトレースを取得するためにセグメンテーション違反で実行を停止し、そしてそれは、これを返さ:Boost :: Regex Segfault(私だと思います)
#0 malloc_consolidate() at /build/eglibc-oGUzwX/eglibc-2.19/malloc/malloc.c:4151
#1 _int_free() at /build/eglibc-oGUzwX/eglibc-2.19/malloc/malloc.c:4057
#2 boost::re_detail::mem_block_cache::~mem_block_cache()() at /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0
#3 __cxa_finalize() at /build/eglibc-oGUzwX/eglibc-2.19/stdlib/cxa_finalize.c:56
#4 ??() at /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.54.0
#5 ??() at
#6 _dl_fini() at /build/eglibc-oGUzwX/eglibc-2.19/elf/dl-fini.c:252
これは私が、私は、ブースト正規表現と間違って何かをしなければならないことを考えさせられましたが、私は私の人生のためにすることはできませんそれを理解する。正規表現を使っているのは、ユーザがたくさんの文字列を入力できるということです。これらの文字列は通常のテキストでも、正規表現でもかまいません。このため、私は基本的にすべての入力と正規表現として対話します。しかし、ユーザーがプレーンテキストとして意図された文字列を与えたが、正規表現とは違った解釈が可能な文字を持っていたらどうなるでしょうか?私がやっているのは、プレーンテキストの入力文字列をすべて調べ、それらの文字をすべてエスケープすることです。
ここに私が作業しているコードがあります。これは私のメインです:
int
main(int argc, char * argv[])
{
// Process input arguments
// The desired input is numVertices (int), graph density (double between 0 and 1), halfLoss (double), total loss (double),
// position expanse (double, m), velocity expanse (double, m/s)
int num_vertices;
double graph_density ;
double half_loss;
double total_loss;
double position_expanse;
double velocity_expanse;
if (argc == 1)
{
num_vertices = 48;
graph_density = 1;
half_loss = 200000;
total_loss = 400000;
position_expanse = 400000;
velocity_expanse = 10000;
}
else
{
if (argc != 7)
{
std::cerr << "Need 6 input arguments" << std::endl;
return 1;
}
std::istringstream ss(argv[1]);
num_vertices;
if (!(ss >> num_vertices))
std::cerr << "First input must be an integer" << std::endl;
graph_density = read_double_input(argv[2]);
half_loss = read_double_input(argv[3]);
total_loss = read_double_input(argv[4]);
position_expanse = read_double_input(argv[5]);
velocity_expanse = read_double_input(argv[6]);
}
// Determine how many edges to create
int num_edges = (int) ((graph_density * num_vertices * (num_vertices - 1)) + 0.5);
// Create the edges
int edges_created = 0;
std::set<std::pair<int, int> > edge_set;
while (edge_set.size() < num_edges)
{
// Pick a random start vertex and end vertex
int start_vertex = rand() % num_vertices;
int end_vertex = rand() % num_vertices;
// Make sure the start and end vertices are not equal
while (start_vertex == end_vertex)
{
end_vertex = rand() % num_vertices;
}
// Insert the new edge into our set of edges
edge_set.insert(std::pair<int, int>(start_vertex, end_vertex));
}
// Create connection handler
ConnectionHandler conn_handler;
// Create lists for from and to vertices
std::vector<std::string> from_list;
std::vector<std::string> to_list;
// Add connections to from and to lists
for (std::set<std::pair<int, int> >::const_iterator edge_it = edge_set.begin(), end_it = edge_set.end(); edge_it != end_it; ++edge_it)
{
int start_vertex = edge_it->first;
int end_vertex = edge_it->second;
from_list.push_back("Radio" + int_to_string(start_vertex));
to_list.push_back("Radio" + int_to_string(end_vertex));
}
// Read the list into the connection handler
conn_handler.read_connection_list(true, from_list, to_list);
return 0;
}
このコードには、私が作成したこのConnectionHandlerオブジェクトがあります。ここではそのためのヘッダだ:そんなにコードのため申し訳ありません
#include <models/network/ConnectionHandler.hpp>
#include <oasis/framework/exceptions/ConfigurationException.h>
#include <boost/regex.hpp>
namespace clcsim
{
ConnectionHandler::
ConnectionHandler()
{
}
ConnectionHandler::
~ConnectionHandler()
{
std::cout << "Destructing conn handler" << std::endl;
}
void
ConnectionHandler::
read_connection_list(
const bool is_white_list,
const std::vector<std::string> &from_radios,
const std::vector<std::string> &to_radios)
{
std::cout << "Reading the connection list" << std::endl;
// Make sure the size of both the input vectors are the same
std::size_t from_radio_size = from_radios.size();
std::size_t to_radio_size = to_radios.size();
if (from_radio_size != to_radio_size)
{
throw ofs::ConfigurationException("Error while initializing the "
"Network model: "
"Connections in from/to lists don't align"
);
}
// Create a regular expression/replacement to find all characters in a non-regular expression
// that would be interpreted as special characters in a regular expression. Replace them with
// escape characters
const boost::regex esc("[.$|()\\[\\]{}*+?\\\\]");
const std::string rep("\\\\&");
// Iterate through the specified connections
for (int i = 0; i < from_radio_size; ++i)
{
std::string from_string = boost::regex_replace(from_radios[i], esc, rep, boost::match_default | boost::format_sed);
std::string to_string = boost::regex_replace(to_radios[i], esc, rep, boost::match_default | boost::format_sed);
//std::cout << "From " << from_string << " to " << to_string << std::endl;
filter_[from_string].insert(to_string);
//filter_[from_radios.at(i)].insert(to_radios.at(i));
}
std::cout << "Got here" << std::endl;
}
} // namespace clcsim
:
#ifndef CLCSIM_CONNECTIONHANDLER_HPP_
#define CLCSIM_CONNECTIONHANDLER_HPP_
#include <models/network/NetworkTypes.hpp>
#include <generated/xsd/NetworkModelInterfaceConfig.hpp>
namespace clcsim
{
typedef std::map<std::string, std::set<std::string> > ConnectionFilter;
class ConnectionHandler
{
public:
ConnectionHandler();
~ConnectionHandler();
void read_connection_list(const bool is_white_list, const std::vector<std::string> &from_radios, const std::vector<std::string> &to_radios);
private:
ConnectionFilter filter_;
std::set<std::string> from_list_;
std::set<std::string> to_list_;
bool is_white_list_;
};
} // namespace clcsim
#endif // CLCSIM_CONNECTIONHANDLER_HPP_
そして、ここではソースです。
boost :: regexでsegfaultsに関連する類似のスレッドがいくつか見えました。これらの例では、ユーザーは正規表現を作成してそれにマッチし、エラーに遭遇した本当にシンプルなコードを持っていました。問題はBoostのバージョン管理に関連していることが判明しました。私はこれらの種類のエラーを再現できるかどうかを試しましたが、これらの簡単な例は私のためにうまくいきました。だから...私はかなり困惑している。私は本当に助けていただきありがとうございます!
「私はセグメンテーションで走りを止めました」という意味が不明です。 segfaultがある場合、*システム*は実行を停止します。デバッガの下で実行している場合、実際には障害の軌跡を示すスタックトレースを取得することができますが、システムが動作する前にプログラムを停止すると、スタックトレースがまだ到着していないセグメンテーションの場所。 –
申し訳ありませんが、ええ、私を修正していただきありがとうございます。私はデバッガの下でそれを実行し、それはエラーで停止し、私にスタックトレースを提供した。 – user2701114
スタックトレースにeglibcがあります。あなたのソフトウェアスタック(boost、C++コンパイラ、C++ランタイムなど)は、すべてeglibc用に構築されていますか?そのglibcの亜種は現在廃止されており、異なるCライブラリに対してビルドされたソフトウェアとライブラリを混在させると問題に陥るかもしれません。 –