Wt :: Http :: Client总是以throw_exception结尾<boost :: bad_weak_ptr>()</boost :: bad_weak_ptr>

时间:2015-03-10 20:47:52

标签: c++ boost wt

我目前正在使用Wt和boost对使用C ++ 11编写的现有程序进行修改。仍然存在一个问题,在使用Wt::Http::Client发布数据时,我总是会遇到异常。

以下是当前代码:

Communication.h

#ifndef COMMUNICATION_H_
#define COMMUNICATION_H_

#include <Wt/WResource>
#include <Wt/Http/Client>
#include <Wt/Http/Message>

class Communication
{
    public:
        static bool sendData(std::string p_sData);
        static void handleHTTPResponse(boost::system::error_code err, const Wt::Http::Message& response);

    private:
        Communication();
        ~Communication();
};

#endif /* COMMUNICATION_H_ */

Communication.cpp

#include "Communication.h"
#include <Wt/WDateTime>
#include "DemtroysUtilitaires.h"

Communication::Communication()
{

}

Communication::~Communication()
{

}

bool Communication::sendData(std::string p_sData)
{
    Wt::Http::Client requete;
    Wt::Http::Message message;
    bool bResult = false;

    std::cout << "NETWORK >>> " << p_sData << std::endl;

    message.addHeader("Content-type","application/json");
    message.addBodyText(p_sData);
    requete.done().connect(boost::bind(&Communication::handleHTTPResponse,_1,_2));

    if(requete.post("http://someserver.com",message))
    {
        bResult = true;
    }

    return bResult;
}

void Communication::handleHTTPResponse(boost::system::error_code err, const Wt::Http::Message& response)
{
    std::cout << "********************************************************************************" << std::endl;
    std::cout << "Communication::handleHttpResponse() status : " << response.status() << std::endl;
    if(err || response.status() != 200)
    {
        std::cerr << "Error: " << err.message() << ", " << response.status() << std::endl;
    }
    std::cout << "********************************************************************************" << std::endl;
}

这是堆栈跟踪:

Thread [15] 28055 [core: 3] (Suspended : Signal : SIGABRT:Aborted)  
    __GI_raise() at raise.c:56 0x7ffff5914cc9   
    __GI_abort() at abort.c:89 0x7ffff59180d8   
    __gnu_cxx::__verbose_terminate_handler() at 0x7ffff5f196b5  
    0x7ffff5f17836  
    std::terminate() at 0x7ffff5f17863  
    __cxa_throw() at 0x7ffff5f17aa2 
    throw_exception<boost::bad_weak_ptr>() at throw_exception.hpp:70 0x7ffff767682c 
    boost::detail::shared_count::shared_count() at shared_count.hpp:580 0x7ffff767682c  
    shared_ptr<Wt::Http::Client::Impl>() at shared_ptr.hpp:404 0x7ffff79201b8   
    shared_from_this() at enable_shared_from_this.hpp:49 0x7ffff79201b8 
    Wt::Http::Client::Impl::startTimer() at Client.C:143 0x7ffff79201b8 
    Wt::Http::Client::Impl::handleResolve() at Client.C:173 0x7ffff7925d2d  
    operator() at mem_fn_template.hpp:280 0x7ffff7924afc    
    operator()<boost::_mfi::mf2<void, Wt::Http::Client::Impl, const boost::system::error_code&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list2<const boost::system::error_code&, const boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>&> > at bind.hpp:392 0x7ffff7924afc   
    operator()<boost::system::error_code, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> > at bind_template.hpp:102 0x7ffff7924afc  
    operator() at bind_handler.hpp:127 0x7ffff7924afc   
    asio_handler_invoke<boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf2<void, Wt::Http::Client::Impl, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Wt::Http::Client::Impl*>, boost::arg<1> (*)(), boost::arg<2> (*)()> >, boost::system::error_code, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> > > at handler_invoke_hook.hpp:69 0x7ffff7924afc  
    invoke<boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf2<void, Wt::Http::Client::Impl, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Wt::Http::Client::Impl*>, boost::arg<1> (*)(), boost::arg<2> (*)()> >, boost::system::error_code, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::bind_t<void, boost::_mfi::mf2<void, Wt::Http::Client::Impl, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Wt::Http::Client::Impl*>, boost::arg<1> (*)(), boost::arg<2> (*)()> > > at handler_invoke_helpers.hpp:37 0x7ffff7924afc 
    boost::asio::detail::resolve_op<boost::asio::ip::tcp, boost::_bi::bind_t<void, boost::_mfi::mf2<void, Wt::Http::Client::Impl, boost::system::error_code const&, boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> >, boost::_bi::list3<boost::_bi::value<Wt::Http::Client::Impl*>, boost::arg<1> (*)(), boost::arg<2> (*)()> > >::do_complete at resolve_op.hpp:112 0x7ffff7924afc 
    complete() at task_io_service_operation.hpp:38 0x7ffff773cfef   
    do_run_one() at task_io_service.ipp:384 0x7ffff773cfef  
    boost::asio::detail::task_io_service::run() at task_io_service.ipp:153 0x7ffff773cfef   
    run() at io_service.ipp:59 0x7ffff773914c   
    Wt::WIOService::run() at WIOService.C:180 0x7ffff773914c    
    0x7ffff6bb8e7a  
    start_thread() at pthread_create.c:312 0x7ffff4da4182   
    clone() at clone.S:111 0x7ffff59d847d

通过分析堆栈跟踪,我非常确定这与done()的{​​{1}}方法无法正确实现,但无法找出原因!

1 个答案:

答案 0 :(得分:2)

我认为问题在于Client类实例requete被声明为局部变量。此外,调用requete.post(...)是异步的,即它将请求保存到Wt的内部队列,并立即结束,而不等待消息传递。因此Communication::sendData方法在Wt内部代码处理队列之前完成。当Wt处理队列时,实例requete已被销毁。由于这个事实,Wt::Http::Client::Impl::startTimer()内的代码无法获取指针并抛出异常。