enable_shared_from_this returns bad_weak_ptr 错误
enable_shared_from_this returns bad_weak_ptr error
我在我的项目中使用了 boost 库。在我编写 rest http 客户端代码时出现此错误。
libc++abi.dylib: 以 std::__1::bad_weak_ptr 类型的未捕获异常终止:bad_weak_ptr
template <typename M>
21 class Http: public std::enable_shared_from_this<Http<M>>
22 {
23 M* message_;
24 bool isSubscribe_;
25 boost::asio::io_context io_;
26 boost::asio::ip::tcp::resolver resolver_;
27 boost::beast::tcp_stream stream_;
28 boost::beast::flat_buffer buffer_;
29 boost::beast::http::request<boost::beast::http::empty_body> req_;
30 boost::beast::http::response<boost::beast::http::string_body> res_;
31 std::function<void(M*)> callback_;
32
33 void SetUpRestHeader() {
34 req_.method(boost::beast::http::verb::get);
35 req_.target(message_ -> header_ -> target_);
36 req_.set(boost::beast::http::field::host, message_ -> header_ -> host_);
37 req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
38
39 resolver_.async_resolve(message_ -> header_ -> host_
40 , message_ -> header_ -> port_
41 , boost::beast::bind_front_handler(
42 &Http::OnResolve
43 , this -> shared_from_this()));
44 }
45
.......
128 explicit Http()
129 :io_{}
130 , resolver_(io_)
131 , stream_(io_)
132 {
133 message_ = new M;
134 }
135
136 M RequestRestMessage()
137 {
138 auto ptr = this -> shared_from_this();
139 isSubscribe_ = false;
140 SetUpRestHeader();
141 }
142 };
151 int main()
152 {
153 Http<binance::ServerTime> http;
154 http.RequestRestMessage();
155 }
即使我没有在构造函数中调用 shared_from_this,也会发生错误。请分享你的智慧。谢谢!
如果你看一下 std::enable_shared_from_this: Examples 你会看到这个:
// Bad: shared_from_this is called without having std::shared_ptr owning the caller
try {
Good not_so_good;
std::shared_ptr<Good> gp1 = not_so_good.getptr();
} catch(std::bad_weak_ptr& e) {
// undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)
std::cout << e.what() << '\n';
}
您的 Http<binance::ServerTime> http;
使用 automatic storage duration 创建了一个对象,因此它(并且不能)由 shared_ptr
拥有。您的 http.RequestRestMessage()
然后调用 this -> shared_from_this
,这就是您收到该错误的原因。
std::enable_shared_from_this::shared_from_this
It is permitted to call shared_from_this
only on a previously shared object, i.e. on an object managed by std::shared_ptr
(in particular, shared_from_this cannot be called during construction of *this).
Otherwise the behavior is undefined (until C++17) std::bad_weak_ptr
is thrown (by the shared_ptr
constructor from a default-constructed weak_this
) (since C++17).
我在我的项目中使用了 boost 库。在我编写 rest http 客户端代码时出现此错误。
libc++abi.dylib: 以 std::__1::bad_weak_ptr 类型的未捕获异常终止:bad_weak_ptr
template <typename M>
21 class Http: public std::enable_shared_from_this<Http<M>>
22 {
23 M* message_;
24 bool isSubscribe_;
25 boost::asio::io_context io_;
26 boost::asio::ip::tcp::resolver resolver_;
27 boost::beast::tcp_stream stream_;
28 boost::beast::flat_buffer buffer_;
29 boost::beast::http::request<boost::beast::http::empty_body> req_;
30 boost::beast::http::response<boost::beast::http::string_body> res_;
31 std::function<void(M*)> callback_;
32
33 void SetUpRestHeader() {
34 req_.method(boost::beast::http::verb::get);
35 req_.target(message_ -> header_ -> target_);
36 req_.set(boost::beast::http::field::host, message_ -> header_ -> host_);
37 req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
38
39 resolver_.async_resolve(message_ -> header_ -> host_
40 , message_ -> header_ -> port_
41 , boost::beast::bind_front_handler(
42 &Http::OnResolve
43 , this -> shared_from_this()));
44 }
45
.......
128 explicit Http()
129 :io_{}
130 , resolver_(io_)
131 , stream_(io_)
132 {
133 message_ = new M;
134 }
135
136 M RequestRestMessage()
137 {
138 auto ptr = this -> shared_from_this();
139 isSubscribe_ = false;
140 SetUpRestHeader();
141 }
142 };
151 int main()
152 {
153 Http<binance::ServerTime> http;
154 http.RequestRestMessage();
155 }
即使我没有在构造函数中调用 shared_from_this,也会发生错误。请分享你的智慧。谢谢!
如果你看一下 std::enable_shared_from_this: Examples 你会看到这个:
// Bad: shared_from_this is called without having std::shared_ptr owning the caller
try {
Good not_so_good;
std::shared_ptr<Good> gp1 = not_so_good.getptr();
} catch(std::bad_weak_ptr& e) {
// undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)
std::cout << e.what() << '\n';
}
您的 Http<binance::ServerTime> http;
使用 automatic storage duration 创建了一个对象,因此它(并且不能)由 shared_ptr
拥有。您的 http.RequestRestMessage()
然后调用 this -> shared_from_this
,这就是您收到该错误的原因。
std::enable_shared_from_this::shared_from_this
It is permitted to call
shared_from_this
only on a previously shared object, i.e. on an object managed bystd::shared_ptr
(in particular, shared_from_this cannot be called during construction of *this).Otherwise the behavior is undefined (until C++17)
std::bad_weak_ptr
is thrown (by theshared_ptr
constructor from a default-constructedweak_this
) (since C++17).