我正在使用独立的Asio和C++11创建一个C++11服务器应用程序,并得到了一个错误,这就是我请求帮助的原因。
误差
在类worker_thread中,在调用shared_from_this()期间,会引发一个bad_weak_ptr异常,这会导致程序崩溃。
布局
connection_manager在std::shared_ptr<worker_thread>容器中创建并存储std::shared_ptr<worker_thread>类型的对象。worker_thread继承自std::enable_shared_from_this<worker_thread>。worker_thread创建类型为std::shared_ptr<connection>的对象。connection需要一个指向类worker_thread的指针(它是一个共享指针),以便in可以调用void handle_finish(std::shared_ptr<connection>)。程序流程
worker_thread是通过构造函数从类connection_manager创建的,使用两个共享指针作为参数的std::make_shared<worker_thread>。void init()是由connection_manager从worker_thread调用的connection_manager从worker_thread调用std::shared_ptr<connection> get_available_connection()connection创建了一个新的std::make_shared<connection>,其中一个参数是通过shared_from_this()获得的current worker_thread的共享指针。shared_from_this()调用期间,程序以bad_weak_ptr异常崩溃。研究
根据我的研究,造成这一错误的最常见原因是:
shared_from_this()时std::shared_ptr时。在我的节目里:
get_available_connection()的调用是分开的,通过终端中的输出行,似乎在调用get_available_connection()时构造和初始化了get_available_connection()。connection_manager类保存一个指向每个worker_thread对象的共享指针。代码
所有的something_ptr something_ptr都是 std::shared_ptr<something>
头文件
connection_manager.hpp
typedef asio::executor_work_guard<asio::io_context::executor_type>
io_context_work;
std::vector<worker_thread_ptr> workers;
std::vector<io_context_ptr> io_contexts;
std::vector<io_context_work> work;worker_thread.hpp
class worker_thread : std::enable_shared_from_this<worker_thread> {
public:
/// Create a worker thread.
explicit worker_thread(io_context_ptr io, config_ptr vars_global);
void init();
void join();
connection_ptr get_available_connection();
//...connection.hpp
explicit connection(std::shared_ptr<worker_thread> worker,
std::shared_ptr<asio::io_context> io,
config_ptr vars_parent);源文件
connection_manager.cpp
connection_manager::connection_manager(config_ptr vars) {
std::size_t number_of_threads = vars->worker_threads;
while(number_of_threads > 0) {
io_context_ptr io_context(new asio::io_context);
io_contexts.push_back(io_context);
work.push_back(asio::make_work_guard(*io_context));
worker_thread_ptr worker =
std::make_shared<worker_thread>(io_context, vars);
workers.push_back(worker);
worker->init();
--number_of_threads;
}
}
connection_ptr connection_manager::get_available_connection() {
std::size_t index_of_min_thread = 0;
std::size_t worker_count = workers.size();
for(std::size_t i = 1; i < worker_count; ++i) {
if(workers[i]->active_connection_count() <
workers[index_of_min_thread]->active_connection_count())
index_of_min_thread = i;
}
return workers[index_of_min_thread]->get_available_connection();
}worker_thread.cpp
worker_thread::worker_thread(io_context_ptr io,
config_ptr vars_global)
:io_context(io), active_conn_count(0), vars(vars_global),
worker(
[this]() {
if(io_context)
io_context->run();
}
) {}
void worker_thread::init() {
//Additional initialisation, this is called by connection_manager
//after this thread's construction
}
connection_ptr worker_thread::get_available_connection() {
connection_ptr conn;
if(!available_connections.empty()) {
conn = available_connections.front();
available_connections.pop();
active_connections.insert(conn);
return conn;
} else {
conn = std::make_shared<connection>(shared_from_this(), io_context, vars);
active_connections.insert(conn);
return conn;
}
}如果这个问题以前得到了回答,我很抱歉,但我试图解决这个问题,在尝试了一段时间之后,我决定寻求帮助更好。
这里的编辑是一个最小的测试,但失败了。它需要CMake,而可能需要更改最低要求的版本。
发布于 2018-05-13 21:14:43
我认为您的问题可能是使用默认的private继承。
下面是一个程序崩溃的简单示例:
class GoodUsage : public std::enable_shared_from_this<GoodUsage>
{
public:
void DoSomething()
{
auto good = shared_from_this();
}
};
class BadUsage : std::enable_shared_from_this<BadUsage> // private inheritance
{
public:
void DoSomething()
{
auto bad = shared_from_this();
}
};
int main()
{
auto good = std::make_shared<GoodUsage>();
auto bad = std::make_shared<BadUsage>();
good->DoSomething(); // ok
bad->DoSomething(); // throws std::bad_weak_ptr
}https://stackoverflow.com/questions/50318414
复制相似问题