I develop a desktop chat with boost asio and beast (for browser support).
But, when building, I have an issue : bad_weak_ptr
, I don't know what is wrong :s Here a link to the source https://onlinegdb.com/BkFhDGHe4
Update1 : I remove run() function into constructor and move it into handle_accept function, tcp_server class. like this:
void tcp_server::handle_accept(const boost::system::error_code ec, websocket_session_ptr new_websocket) { if (!ec) { // Happens when the timer closes the socket if(ec == boost::asio::error::operation_aborted) return; new_websocket->run(); //Here chatwebsocketsessionpointer session = chat_websocket_session::create(room, new_websocket); room->join(session); wait_for_connection(); } }
void tcp_server::handle_accept(const boost::system::error_code ec, websocket_session_ptr new_websocket) { if (!ec) { // Happens when the timer closes the socket if(ec == boost::asio::error::operation_aborted) return; new_websocket->run(); //Here chatwebsocketsessionpointer session = chat_websocket_session::create(room, new_websocket); room->join(session); wait_for_connection(); } }
I can see the chat_webocket_session is deleted, but still have issue with bad_weak_ptr
Update 2 : I found where is the issue. If I never call do_read() function, there is no error, and I can connect to server with ws If I call it into wait_for_data from chat_websoket_session class, I have issue. So I must found how call do_read()
Update 3 : If I do websocket_session_ptr new_websocket(new websocket_session(std::move(socket))); acceptor.async_accept( socket, boost::bind( &tcp_server::websocket_accept, this, boost::asio::placeholders::error, new_websocket ));
websocket_session_ptr new_websocket(new websocket_session(std::move(socket))); acceptor.async_accept( socket, boost::bind( &tcp_server::websocket_accept, this, boost::asio::placeholders::error, new_websocket ));
making ref to : boost beast websocket example , I accept first the socket, and after I accept the websocket with m_ws.async_accept()
but I have now Bad file descriptor
which means the socket is not open.
PS: I update the ide URL (GDB online debugger)
You're using the shared pointer to this from inside the constructor:
websocket_session::websocket_session(tcp::socket socket)
: m_ws(std::move(socket))
, strand(socket.get_executor())
{
run();
}
Inside run()
you do
void websocket_session::run() {
// Accept the websocket handshake
std::cout << "Accepted connection" << std::endl;
m_ws.async_accept(boost::asio::bind_executor(
strand, std::bind(&websocket_session::on_accept, , std::placeholders::_1)));
}
That uses shared_from_this()
which will try to lock the unitialized weak_ptr
from enable_shared_from_this
. As you can see in the documentation that throws the std::bad_weak_ptr
exception (ad. 11)
The documentation to shared_from_this
explicitly warns against this:
It is permitted to call shared_from_this only on a previously shared object, ie on an object managed by std::shared_ptr ( in particular, shared_from_this cannot be called in a constructor ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.