【问题标题】:Getting 'bad_weak_ptr' error出现“bad_weak_ptr”错误
【发布时间】:2016-06-04 13:06:29
【问题描述】:

我试图将一个共享指针从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();
}

【问题讨论】:

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


    【解决方案1】:

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

    这是真的,在对象完全构造之前,您不能使用shared_from_this()。仅在那时分配底层weak_ptr。限制不仅仅是来自构造函数的直接 - 在 QTGUI 完全构造之前,您仍在调用 shared_from_this。您使用函数从不同的函数中检索它的事实并不重要。

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


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

    【讨论】:

    • 另一个注意事项:要使shared_from_this 工作,必须 shared_ptr 已经拥有该对象,否则弱引用将威胁对象无效。这就是对构造函数的限制的来源:它拥有shared_ptr,它正确设置了弱引用,并且在对象完全构造之前它无法工作。
    • 感谢两位的回答。我尝试从main 调用start 函数——我在问题中包含了代码——但它仍然给我同样的错误。我在调试器中看到它在我在 start 函数中调用 shared_from_this 的确切位置给了我错误。这是为什么呢?
    • @StrikeTeam 因为w 不是shared_ptr
    • @Barry 我接受您的回答,因为我认为问题已得到解答,但我有点复杂。 QTGUI w; 不能是 shared_ptr,否则应用程序会给我一个 Assertion px !=0 失败。我猜是因为Qts 内部管理什么的。有没有办法以某种方式将类的指针传递给Client
    • @Strike 嗯?只需将其设为shared_ptrshared_ptr&lt;QTGUI&gt; w = make_shared&lt;QTGUI&gt;();
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    相关资源
    最近更新 更多