我有一个使用cpprestsdk
查询http和订阅数据流的websocketpp
程序。该程序将立即崩溃(它说Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
)。但是,如果我评论http查询或添加数据流,程序就不会崩溃。
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include "json.hpp"
#include <iostream>
#include <ctime>
#include <iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <vector>
#include <string>
using std::string;
using namespace web;
using std::cout, std::endl;
using std::vector;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_stream_data(websocketpp::connection_hdl hdl, message_ptr msg) {
}
class OrderBook {
public:
void initialize() {
web::http::client::http_client_config cfg;
std::string uri = string("https://fapi.binance.com/fapi/v1/depth?symbol=btcusdt&limit=1000");
web::http::client::http_client client(U(uri), cfg);
web::http::http_request request(web::http::methods::GET);
request.headers().add("Content-Type", "application/x-www-form-urlencoded");
web::http::http_response response = client.request(request).get();
}
int start_stream() {
client c;
std::string uri = string("wss://fstream.binance.com/ws/btcusdt@depth@100ms");
try {
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
c.init_asio();
c.set_message_handler(bind(on_stream_data, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
c.connect(con);
c.run();
} catch (websocketpp::exception const &e) {
std::cout << e.what() << std::endl;
}
}
};
int main(int argc, char *argv[]) {
OrderBook ob;
ob.initialize(); // comment either of these two lines, the program won't crash, otherwise the program will crash once start
std::this_thread::sleep_for(std::chrono::milliseconds(10000000));
ob.start_stream(); // comment either of these two lines, the program won't crash, otherwise the program will crash once start
}
当我在Clion调试模式下运行这个程序时,Clion显示崩溃来自于/opt/homebrew/Cellar/boost/1.76.0/include/boost/asio/ssl/detail/impl/engine.ipp
中的函数。
int engine::do_connect(void*, std::size_t)
{
return ::SSL_connect(ssl_);
}
上面写着Exception: EXC_BAD_ACCESS (code=1, address=0xf000000000)
它有什么问题?这是因为我使用boost::asio运行了两段代码,并且某些东西不应该被初始化两次?
发布于 2022-05-22 11:46:56
我可以编译它并运行它。
我的最佳选择是,您可能是混合版本,特别是增强版本。当网上解决违规行为导致未定义行为时,会导致常见的故障模式。
请注意,这些标头专用库依赖于许多不是头库的boost库(例如Boost System、Thread和/或Chrono)。您需要针对与链接库相同的版本进行编译。
如果您使用任何库的发行版打包版本(cpprestsdk、websocketpp或您正在使用的任何json库),那么使用Boost的发行版打包版本也是最安全的。
我会用Boost (Beast /websocket,Json,您猜到了)来简化这种情况。
在OS 1.65版本的测试Ubuntu18.04上运行所有这些,start_stream
序列将触发此信息错误:
[2022-05-22 13:42:11] [fatal] Required tls_init handler not present.
could not create connection because: Connection creation attempt failed
当UBSAN/ASAN干净的时候。一旦您发现导致程序崩溃的配置问题,该错误可能会对您有所帮助。
https://stackoverflow.com/questions/72336384
复制相似问题