获得' bad_weak_ptr'错误

时间:2016-06-04 13:06:30

标签: 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();
}

1 个答案:

答案 0 :(得分:2)

  

我已经读过,我不能直接在构造函数中分配shared_from_this(),因为指针在那时还没有准备好。

确实如此,在完全构造对象之前,不能使用shared_from_this()。仅在此时才指定基础weak_ptr。限制不仅仅是直接来自构造函数 - 您在shared_from_this完全构造之前仍然在调用QTGUI。您使用函数从不同的函数中检索它的事实并不重要。

创建后,只需在课程外部调用start()即可。那将是有效的。

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