简体   繁体   English

带有boost智能指针的bad_weak_ptr

[英]bad_weak_ptr with boost smart pointer

I develop a desktop chat with boost asio and beast (for browser support). 我使用boost asio和beast开发桌面聊天(用于浏览器支持)。

I use this architecture : 我用这个架构: 在此输入图像描述

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 但是,在构建时,我有一个问题: bad_weak_ptr ,我不知bad_weak_ptr什么问题:s这里是源代码的链接https://onlinegdb.com/BkFhDGHe4

Update1 : I remove run() function into constructor and move it into handle_accept function, tcp_server class. Update1 :我将run()函数删除到构造函数中并将其移动到handle_accept函数tcp_server类中。 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 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(); } }我可以看到chat_webocket_session被删除,但仍然有问题与bad_weak_ptr

Update 2 : I found where is the issue. 更新2:我发现了问题所在。 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. 如果我从不调用do_read()函数,则没有错误,我可以用ws连接到服务器如果我从chat_websoket_session类调用wait_for_data,我就有问题了。 So I must found how call do_read() 所以我必须找到如何调用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 )); 更新3:如果我做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. 使ref成为: boost beast websocket示例 ,我首先接受套接字,然后我接受带有m_ws.async_accept()的websocket但我现在有了Bad file descriptor ,这意味着套接字没有打开。

PS: I update the ide URL (GDB online debugger) PS:我更新了ide URL(GDB在线调试器)

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 在里面run()你做

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 . 这使用shared_from_this() ,它将尝试从enable_shared_from_this锁定unitialized weak_ptr As you can see in the documentation that throws the std::bad_weak_ptr exception (ad. 11) 正如您在文档中看到的那样抛出std::bad_weak_ptr异常( std::bad_weak_ptr

The documentation to shared_from_this explicitly warns against this: shared_from_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 ). 允许仅在先前共享的对象上调用shared_from_this,即在由std :: shared_ptr管理的对象上( 特别是,在构造函数中不能调用shared_from_this )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM