c++ - 函数返回时的 Cpp 调试断言错误
问题描述
我正在尝试实现一个使用 google protobuf 格式流式传输数据的 Boost websocket 服务。我已经使用 .h 和 .cpp 文件实现了一个非常简单的库,但是从 cryptowatch_API.cpp 退出 connect() 时出现以下错误(检查了刹车点)。
调试断言失败!
文件:debug_heap.cpp 行:908 表达式:is_block_type_valid(header -> _block_use)
最奇怪的是,代码每隔一段时间就会一直运行到最后没有问题……有人知道这里可能出了什么问题吗?我的文件发布在下面,我正在使用 VS2019。任何帮助将非常感激!我已经为此苦苦挣扎了一段时间...
#include <boost/beast/core.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include "public/stream/stream.pb.h"
#include "cryptowatch_API.h"
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
// Sends a WebSocket message and prints the response
int main()
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
const char* API_key = "INSERT_API_KEY";
Cryptowatch_API cryptowatch_API(API_key);
ProtobufStream::AuthenticationResult::Status auth_status;
auth_status = cryptowatch_API.connect();
/*
if (auth_status == ProtobufStream::AuthenticationResult::AUTHENTICATED) {
std::cout << "Authentication successful" << std::endl;
}
else {
//std::cout << "Authentication failed: " << auth_status << std::endl;
}
*/
cryptowatch_API.disconnect();
return EXIT_SUCCESS;
}
.h 文件:
// cryptowatch_API.h
#ifndef Cryptowatch_API_H // include guard
#define Cryptowatch_API_H
class Cryptowatch_API
{
private:
const char* API_key;
boost::beast::websocket::stream<boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>* ws;
public:
Cryptowatch_API(const char*);
ProtobufStream::AuthenticationResult::Status connect();
void disconnect();
};
#endif Cryptowatch_API_H
.cpp 文件:
// cryptowatch_API.cpp
#include <boost/beast/core.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include "public/stream/stream.pb.h"
#include "cryptowatch_API.h"
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
Cryptowatch_API::Cryptowatch_API(const char* API_key) {
Cryptowatch_API::API_key = API_key;
}
void Cryptowatch_API::disconnect() {
//ws->close(websocket::close_code::normal);
}
//Will return TRUE when connect was successful, FALSE when unsuccessful
ProtobufStream::AuthenticationResult::Status Cryptowatch_API::connect() {
// Check command line arguments.
auto const host = "stream.cryptowat.ch";
auto const port = "443";
// The io_context is required for all I/O
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ ssl::context::tlsv12_client };
// This holds the root certificate used for verification
//load_root_certificates(ctx);
// These objects perform our I/O
tcp::resolver resolver{ ioc };
websocket::stream<beast::ssl_stream<tcp::socket>> webs{ ioc, ctx };
//Dereference the websocket object to store it in Cryptowatch_API
ws = &webs;
// Look up the domain name
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
net::connect(ws->next_layer().next_layer(), results.begin(), results.end());
// Perform the SSL handshake
ws->next_layer().handshake(ssl::stream_base::client);
// Set a decorator to change the User-Agent of the handshake
ws->set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the websocket handshake
std::string connect_url_addition = "/connect?format=binary&apikey=";
connect_url_addition.append(API_key);
ws->handshake(host, connect_url_addition);
// This buffer will hold the incoming message
beast::flat_buffer buffer;
ProtobufStream::StreamMessage streamMessage;
//streamMessage.InitAsDefaultInstance();
while (streamMessage.has_authenticationresult() == FALSE) {
buffer.clear();
if (ws->read(buffer) > 0) {
//Parse streamMessage from the websocket input message
streamMessage.ParseFromArray(static_cast<char const*>(buffer.data().data()), buffer.data().size());
}
}
ProtobufStream::AuthenticationResult::Status authenticated;
authenticated = streamMessage.authenticationresult().status();
return authenticated;
}
解决方案
推荐阅读
- python - 在没有内部的情况下抓取 HTML 标记的主要文本内容
- c# - 如何交换矩阵二维数组中的元素?
- c++ - 从无向图中的树根检测循环
- javascript - Firebase 存储连接到 Angular 应用
- c# - 无法摆脱 customErrors ASP.NET
- swiftui - SwiftUI ScrollView 也触发 DragGesture
- java - 在 OOP 编程中,在其方法中引用子接口是正确的方法吗?
- java - 插件 'org.apache.maven.plugins:maven-compiler-plugin:3.8.0' 未找到
- db2 - 将文件中的 TIMESTAMP 数据加载到 IBM Db2 on Cloud
- postgresql - Heroku 错误:sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:连接被拒绝