运行时错误:terminate called after throwing an instance of 'std::bad_weak_ptr'what(): bad_weak_ptr, Aborted (core dumped)

报这个错的原因是类Session继承了std::enable_shared_from_this<Session>,其构造函数中尚未初始化*this,但是在构造函数中调用了shared_from_this(),将其初始化逻辑移到单独方法即可解决

class Session: public std::enable_shared_from_this<Session>{
	public:
		Session(){
			auto self = shared_from_this();
		}
}
posted @ 2025-03-09 23:37  正明小佐  阅读(363)  评论(0)    收藏  举报