我正在尝试在Ubuntu 19.04上的给定目录中读取文件。我打算在标准库中使用directory_iterator。我正在使用CLion IDE,它在目录“ usr / bin / c ++”中使用编译器。我猜这是g ++编译器,我系统中的g ++版本是8.3。我正在使用的C ++版本是C ++ 17。
我能够在Windows 10上的Visual Studio 2017上成功运行代码,但是在使用CLion时,代码在Ubuntu上产生了分段错误错误。以下代码来自cppreference页面中有关directory_iterator的信息。
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::create_directories("sandbox/a/b");
std::ofstream("sandbox/file1.txt");
std::ofstream("sandbox/file2.txt");
for(auto& p: fs::directory_iterator("sandbox"))
std::cout << p.path() << '\n';
fs::remove_all("sandbox");
}
Run Code Online (Sandbox Code Playgroud)
我希望代码可以打印给定文件夹中的文件,但我却得到了Segfault。
该过程以退出代码139(被信号11:SIGSEGV中断)完成。
这个问题背后的原因可能是什么?问题可能与CMake有关吗?
当我优雅地关闭连接到它的客户端时,我的服务器崩溃了,而客户端正在接收大量数据。我正在考虑一个可能的终生错误,就像 boost ASIO 中的大多数错误一样,但是我自己无法指出我的错误。
每个客户端与服务器建立 2 个连接,其中一个用于同步,另一个连接是长期连接,用于接收持续更新。在“同步阶段”,客户端接收大量数据以与服务器状态同步(“状态”基本上是 JSON 格式的数据库数据)。同步完成后,同步连接将关闭。客户端通过其他连接接收数据库的更新(与“同步数据”相比,这些数据当然是非常小的数据)。
这些是相关文件:
连接.h
#pragma once
#include <array>
#include <memory>
#include <string>
#include <boost/asio.hpp>
class ConnectionManager;
/// Represents a single connection from a client.
class Connection : public std::enable_shared_from_this<Connection>
{
public:
Connection(const Connection&) = delete;
Connection& operator=(const Connection&) = delete;
/// Construct a connection with the given socket.
explicit Connection(boost::asio::ip::tcp::socket socket, ConnectionManager& manager);
/// Start the first asynchronous operation for the connection.
void start();
/// Stop all asynchronous operations associated with the connection. …Run Code Online (Sandbox Code Playgroud)