c++ - 出现 'bad_weak_ptr' 错误

标签 c++ boost shared-ptr weak-ptr

我试图将一个共享指针从 QTGUI 类传递到 Client 类,但不断收到 bad Weak ptr 错误。我读到我无法直接在构造函数中分配 shared_from_this() ,因为此时指针尚未准备好。

因此,我创建了一个 getptr 函数,它返回对象的指针,以便稍后在进一步的调用中将其分配给客户端。但它似乎也不起作用。

我在这里缺少什么?预先非常感谢您。

qtgui.hpp

#ifndef QTGUI_H
#define QTGUI_H

#include <QMainWindow>
#include <QFileSystemWatcher>

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include <boost/bind.hpp>

class Client;

namespace Ui {
class QTGUI;
}

class QTGUI : public QMainWindow, public boost::enable_shared_from_this<QTGUI>
{
    Q_OBJECT

public:
    explicit QTGUI(QWidget *parent = 0);
    ~QTGUI();

    void start();
    boost::shared_ptr<QTGUI> getptr();

    /* Signals */
    typedef boost::signals2::signal
        <void (const char* ipaddress, const char* port)> start_connect;
    typedef start_connect::slot_type start_connect_st;

    boost::signals2::connection start_connect_ui(const start_connect_st& slot);

private slots:
private:
    Ui::QTGUI *ui;

    boost::shared_ptr<Client> client_;

    start_connect sc_signal;
};

#endif // QTGUI_H

qtgui.cpp

#include "qtgui.h"
#include "ui_qtgui.h"

#include <iostream>

#include <QHostAddress>
#include <QRegExp>
#include <QIntValidator>
#include <QFileDialog>

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

#include "../client/client.hpp"


#include <boost/bind.hpp>
#include <boost/thread.hpp>

QTGUI::QTGUI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QTGUI),
    connected_(false),
    client_(new Client)
{


    start();
}

boost::shared_ptr< QTGUI > QTGUI::getptr()
{
    return shared_from_this();
}

void QTGUI::start()
{

    client_->addui(getptr());
    client_->get_con_status(boost::bind(&QTGUI::connection_status, this, 
_1));
}

boost::signals2::connection QTGUI::start_connect_ui(const start_connect_st& 
slot)
{
    return sc_signal.connect(slot);
}

client.hpp

#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>

#include <boost/signals2.hpp>

#include "../common/message.hpp"

class QTGUI;

class Client : public boost::enable_shared_from_this<Client>
{
public:
    Client(/*boost::shared_ptr<QTGUI> ui*/);

    void addui(boost::shared_ptr<QTGUI> ptr);


    /* Signals */
    typedef boost::signals2::signal<void (bool status)> connect_status;
    typedef connect_status::slot_type connect_status_st;

    boost::signals2::connection get_con_status(const connect_status_st& slot);

private:
    boost::shared_ptr<QTGUI> ui_;
    boost::shared_ptr<boost::thread> thread_;
    boost::shared_ptr<boost::asio::io_service> io_service_;
    boost::shared_ptr<boost::asio::io_service::work> work_;
    boost::shared_ptr<boost::asio::ip::tcp::socket> socket_;

    connect_status cs_sig;
};

#endif//  client.hpp 

client.cpp

#include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <boost/function.hpp>

#include <cstdio> /* sprintf */

#include "client.hpp"
#include "../common/commands.hpp"
#include "../common/kinotify.hpp"
#include "../gui/qtgui.h"

#include <functional> 
#include <unistd.h>

Client::Client(/*boost::shared_ptr<QTGUI> ui*/):
//ui_(ui),
io_service_(new boost::asio::io_service),
work_(new boost::asio::io_service::work(*io_service_)),
connected_(false)
{
    start();
}
void Client::addui(boost::shared_ptr< QTGUI > ptr)
{
    ui_ = ptr;
    std::cout << "Connecting..." << std::endl;
    ui_->start_connect_ui(boost::bind(&Client::post_connect, 
                                      shared_from_this(), _1, _2));
}


void Client::start()
{
    if(thread_)
        return;

    std::cout << "Creating new thread bruh" << std::endl;
    thread_.reset(new boost::thread(
        boost::bind(&boost::asio::io_service::run, io_service_)
    ));
    std::cout << "start Thread ID [" << boost::this_thread::get_id() << 
    "]" << std::endl;
}

void Client::stop()
{
    if(!thread_)
        return;

    std::cout << "Ending" << std::endl;
    socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
    socket_->close();
    io_service_->stop();
    thread_->join();
    io_service_->reset();
    thread_.reset();
}

boost::signals2::connection Client::get_con_status(const connect_status_st& 
slot)
{
    return cs_sig.connect(slot);
}

编辑:添加main.cpp

#include "qtgui.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTGUI w;
    w.start();
    w.show();

    return a.exec();
}

最佳答案

I have read that I can't assign an shared_from_this() directly in a constructor, because the pointer is not ready at that point.

确实如此,在对象完全构造完成之前,您不能使用shared_from_this()。仅在此时,底层的 weak_ptr 才会被分配。该限制不仅仅直接来自构造函数 - 您仍在完全构建 QTGUI 之前调用 shared_from_this。事实上,您使用一个函数从另一个函数中检索它并不重要。

创建类后,只需在类外部调用 start() 即可。这样就可以了。


旁注:getptr() 没有用。只需直接使用 shared_from_this() 即可。

关于c++ - 出现 'bad_weak_ptr' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630593/

相关文章:

C++ 复制构造函数采用错误的值

c++ - 将字符串设置为 QDoubleSpinBox

c++ - 尝试维基百科查询时出现 CURLPP 400 错误请求

c++ - 将 <N> 传递给回调函数的模板化迭代器

c++ - 是否存在从 std::shared_ptr<T> 到 std::shared_ptr<const T> 的隐式转换?

c++ - boost.enable_shared_from_this 并创建另一个 shared_ptr<T>

c++ - x265编码器,将yuv转为x265_picture

python - C++ 中基本 Boost 使用的段错误

c++ - Boost::‘operator[]’ 的函数错误模糊重载

c++ - 关于shared_ptr、scoped_ptr和shared_array的一些问题