shared_from_this() 会导致 std::bad_weak_ptr,即使正确使用 make_shared

shared_from_this() causes std::bad_weak_ptr even when correctly using make_shared

本文关键字:shared from make ptr weak std bad this      更新时间:2023-10-16

我正在使用独立的 Asio 和 C++11 创建一个C++服务器应用程序,但收到错误,这就是我寻求帮助的原因。

错误

在类worker_thread中,在调用shared_from_this()期间,会引发bad_weak_ptr异常,导致程序崩溃。

布局

  1. connection_manager创建对象并将其存储在std::vector容器中std::shared_ptr<worker_thread>类型的对象
  2. worker_thread继承自std::enable_shared_from_this<worker_thread>的类。
  3. worker_thread创建类型为std::shared_ptr<connection>的对象。
  4. connection需要一个指向类worker_thread的指针(这是一个共享指针(,以便 in 可以调用void handle_finish(std::shared_ptr<connection>)

程序流程

  1. worker_thread是通过其构造函数创建的,从类connection_manager使用带有两个共享指针作为参数的std::make_shared<worker_thread>创建。
  2. void init()connection_managerworker_thread调用
  3. 在程序的后面,connection_managerworker_thread调用std::shared_ptr<connection> get_available_connection()
  4. 在此方法的执行过程中,通过std::make_shared<connection>创建一个新connection,其中一个参数是指向通过shared_from_this()获得的当前worker_thread的共享指针
  5. shared_from_this()调用期间,程序崩溃并出现bad_weak_ptr异常。

研究

根据我的研究,此错误的最常见原因是:

在构造函数
  1. (或由构造函数调用的函数(中调用shared_from_this()
  2. 当没有指向对象的现有std::shared_ptr时。

在我的程序中:

  1. 对构造函数和get_available_connection()的调用是分开的,通过终端中的输出行,似乎在调用get_available_connection()时构造和初始化worker_thread
  2. connection_manager类保存指向每个worker_thread对象的共享指针。

法典

所有something_ptrstd::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,您可能需要更改所需的最低版本

谷歌云端硬盘链接

我认为您的问题可能是您使用默认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    
}