Wt::Http::Client 总是以 throw_exception<boost::bad_weak_ptr>() 结尾
Posted
技术标签:
【中文标题】Wt::Http::Client 总是以 throw_exception<boost::bad_weak_ptr>() 结尾【英文标题】:Wt::Http::Client always end with throw_exception<boost::bad_weak_ptr>() 【发布时间】:2015-03-10 20:47:51 【问题描述】:我目前正在使用 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
通过分析堆栈跟踪,我很确定这与 Wt::Http::Client
中的 done()
方法有关,该方法未正确实现,但无法弄清楚原因!
【问题讨论】:
【参考方案1】:我认为问题在于 Client 类实例 requete
被声明为局部变量。同样,调用requete.post(...)
是异步的,即它将请求保存到Wt
的内部队列并立即完成,而无需等待消息传递。因此Communication::sendData
方法在Wt
内部代码处理队列之前完成。当Wt
处理队列时,实例requete
已经被销毁。由于这个事实,Wt::Http::Client::Impl::startTimer()
内部的代码无法获取指针并引发异常。
【讨论】:
如果我将requete
变量更改为静态变量,应该可以吗?
@AlexandreLavoie,我不知道 Wt 在静态变量的情况下会如何表现。 An example of the documentation 通过指针处理分配的对象。
那是我的第二个选择,我会在今天晚些时候或明天尝试它们,但我很确定你找到了正确的方法,很快就会给你消息!
在示例中使用指针变量进行测试,到目前为止它正在工作,而不是你让我走上正轨!以上是关于Wt::Http::Client 总是以 throw_exception<boost::bad_weak_ptr>() 结尾的主要内容,如果未能解决你的问题,请参考以下文章