2016-12-06 37 views
4

我遇到了一些我正在運行的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對象,我創建。下面是該頭:

#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_ 

而這裏的源:

#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 

對不起這麼多的代碼。

我看到一些類似的線程與boost :: regex的segfaults相關。在這些例子中,用戶擁有非常簡單的代碼,只是創建了一個正則表達式並將其匹配併發生錯誤。原來,這個問題與Boost版本控制有關。我試圖看看是否可以複製這些類型的錯誤,但這些簡單的例子對我來說工作得很好。所以......我很難過。我非常感謝任何幫助!

+1

我不確定你的意思是「我停止了運行在段錯誤」。如果你有段錯誤,那麼*系統*將停止運行。如果你在一個調試器下運行,那麼你確實應該能夠得到一個顯示錯誤軌跡的堆棧軌跡,但是如果你在系統執行之前停止了這個程序,那麼你就不能知道你的堆棧軌跡表徵了尚未到來的段錯誤的位置。 –

+0

對不起,是的,謝謝你糾正我。我在調試器下運行它,並在錯誤處停止併爲我提供堆棧跟蹤。 – user2701114

+1

我在你的堆棧跟蹤中看到eglibc。您的軟件堆棧(boost,C++編譯器,C++運行庫等)在爲eglibc構建時是否一致?這個glibc變體現在已經停止了,如果你混合使用針對不同C庫構建的軟件和庫,你可能會遇到麻煩。 –

回答

0

爲了將其從「未答覆」列表中刪除,我將發佈在評論中提供的答案,而不是在這裏。 OP確定,Boost與eglibc鏈接的建議確實與其他與glibc鏈接的代碼相沖突。因此,OP發現升級他的操作系統,使eglibc鏈接庫不再被使用,解決了這個問題。