shared_from_this() 会导致 std::bad_weak_ptr,即使正确使用 make_shared
shared_from_this() causes std::bad_weak_ptr even when correctly using make_shared
我正在使用独立的 Asio 和 C++11 创建一个C++服务器应用程序,但收到错误,这就是我寻求帮助的原因。
错误
在类worker_thread
中,在调用shared_from_this()
期间,会引发bad_weak_ptr
异常,导致程序崩溃。
布局
- 类
connection_manager
创建对象并将其存储在std::vector
容器中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()
- 在此方法的执行过程中,通过
std::make_shared<connection>
创建一个新connection
,其中一个参数是指向通过shared_from_this()
获得的当前worker_thread
的共享指针 - 在
shared_from_this()
调用期间,程序崩溃并出现bad_weak_ptr
异常。
研究
根据我的研究,此错误的最常见原因是:
在构造函数- (或由构造函数调用的函数(中调用
shared_from_this()
时 - 当没有指向对象的现有
std::shared_ptr
时。
在我的程序中:
- 对构造函数和
get_available_connection()
的调用是分开的,通过终端中的输出行,似乎在调用get_available_connection()
时构造和初始化worker_thread
connection_manager
类保存指向每个worker_thread
对象的共享指针。
法典
所有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,您可能需要更改所需的最低版本。
谷歌云端硬盘链接
我认为您的问题可能是您使用默认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
}
相关文章:
- 如何解决"invalid conversion from 'char' to 'const char*'"
- std::async from std::async in windows xp
- 引用 std::shared:ptr 以避免引用计数
- std::is_reference from std::any
- std::time_point from and to std::string
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- std::chrono::time_point from std::string
- Visual accept std::string from std::byte iterator
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++ 中的"template <typename From, typename Tag> struct Alias;"是什么?
- from std::vector to adept::avector
- qt get child (Callout) from QChart
- Webassembly from Javascript
- Generate boost::uuids::uuid from boost::compute::detail::sha
- 无法使用 libtool 将 -shared 参数传递给 g++
- 什么是"Reading unbounded stream from standard input (Memory Management)"的例子
- Calling C++ dll from python
- 为什么我会收到"invalid conversion from 'Queue*/Stack*' to 'int'"错误消息?
- std::chrono 在从 main 或 from 类方法使用时给出不同的值