C ++ bad_weak_ptr错误 [英] c++ bad_weak_ptr error

查看:178
本文介绍了C ++ bad_weak_ptr错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造一些定时类,它打印文本每隔N秒,其中N将在构造函数中初始化。

I want to create some Timer class, which prints "text" every N seconds, where N will be initialized in constructor.

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <iostream>

class Timer : public boost::enable_shared_from_this<Timer>
{
public:
    Timer ( const double interval ) : interval_sec( interval )
    {
        io_service_ = new boost::asio::io_service;
        timer_      = new boost::asio::deadline_timer ( * io_service_ );
        start( );
        io_service_ -> run( );
    }

    void start ( )
    {
        timer_ -> expires_from_now( boost::posix_time::seconds( 0 ) );
        timer_ -> async_wait(boost::bind( &Timer::handler
                                        , shared_from_this( )
                                        , boost::asio::placeholders::error
                                        )
                            );
    }

private:
    void handler( const boost::system::error_code& error )
    {
        if ( error )
        {
            std::cerr << error.message( ) << std::endl;
            return;
        }

        printf( "text\n" );
        timer_ -> expires_from_now( boost::posix_time::seconds( interval_sec ) );
        timer_ -> async_wait( boost::bind( &Timer::handler
                                         , shared_from_this( )
                                         , boost::asio::placeholders::error
                                         )
                            );
    }

private:
    boost::asio::io_service * io_service_;
    boost::asio::deadline_timer * timer_;
    double interval_sec;
};

int main()
{
    boost::shared_ptr<Timer> timer( new Timer ( 10 ) );
    return 0;
}

但我有 bad_weak_ptr 错误。

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
  what():  tr1::bad_weak_ptr
Aborted

我是什么做错了,我该如何解决这个问题?

What am I doing wrong and how can I fix it?

推荐答案

问题是最有可能是你不能使用 shared_from_this 直到对象实际上是由一个共享指针管理。一般来说它不是启动一个线程或异步服务在构造一个好主意,因为你可能会不吉利,并在构造函数完成之前,新线程可能会启动,从而执行一个不完全构造的对象。

The problem is most probably that you cannot use shared_from_this until the object is actually managed by a shared pointer. In general it is not a good idea to launch a thread or asynchronous service in the constructor, as you might be unlucky and the new thread might start before the constructor has completed and thus execute on a not fully constructed object.

在您的特定情况下,它甚至更糟,因为你进入了你的定时类的构造函数中的事件循环,这意味着控制永远不会返回到,对象从不被管理的的shared_ptr 主...

In your particular case it is even worse, as you are entering the event loop inside the constructor of your Timer class, and that means that control never returns to main, the object is never managed by the shared_ptr in main...

您应该移动调用启动并调用的run()来不同的功能和呼叫从之后的对象在实际管理的的shared_ptr

You should move the call to start and the call to run() to a different function, and call that from main after the object is actually managed in the shared_ptr.

这篇关于C ++ bad_weak_ptr错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆