与boost::property_tree XML解析器一起使用时,boost::协同程序库崩溃
Crash in boost::coroutine library when used alongside boost::property_tree XML parser
我使用Simple Web Server库创建简单的Web服务,将XML转换为JSON,反之亦然。反过来,它使用了几个boost库以及其中的boost::coroutine。对于XML<->JSON转换我使用boost::property_tree库进行中间表示。这是代码:
#include <iostream>
#include <sstream>
#include <server_http.hpp>
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace std;
using namespace boost::property_tree;
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
int main()
{
HttpServer server(8080, 1);
server.resource["^/json_to_xml$"]["POST"] = [](auto& response, auto request) {
try
{
ptree pt;
read_json(request->content, pt);
ostringstream json, xml;
write_json(json, pt);
clog << "JSON request content:" << endl << json.str() << endl;
write_xml(xml, pt, xml_writer_make_settings<ptree::key_type>(' ', 1u));
clog << "XML response content:" << endl << xml.str() << endl;
response << "HTTP/1.1 200 OKrnContent-Length: " << xml.str().length() << "rnrn" << xml.str();
}
catch(const exception& e)
{
cerr << "Error:" << endl << e.what() << endl;
response << "HTTP/1.1 400 Bad RequestrnContent-Length: " << strlen(e.what()) << "rnrn" << e.what();
}
};
server.resource["^/xml_to_json$"]["POST"] = [](auto& response, auto request) {
try
{
ptree pt;
read_xml(request->content, pt, xml_parser::trim_whitespace | xml_parser::no_comments);
ostringstream xml, json;
write_xml(xml, pt, xml_writer_make_settings<ptree::key_type>(' ', 1u));
clog << "XML request content:" << endl << xml.str() << endl;
write_json(json, pt);
clog << "JSON response content: " << endl << json.str() << endl;
response << "HTTP/1.1 200 OKrnContent-Length: " << json.str().length() << "rnrn" << json.str();
}
catch(const exception& e)
{
cerr << "Error:" << endl << e.what() << endl;
response << "HTTP/1.1 400 Bad RequestrnContent-Length: " << strlen(e.what()) << "rnrn" << e.what();
}
};
server.start();
return 0;
}
JSON到XML的转换工作正常,但相反会导致程序崩溃。当我在服务器的回调中不使用boost::property_tree的XML解析器时,程序运行良好。这是执行的结果。这是GDB回溯。最后是Valgrind输出。
boost库的使用版本为1.58.0,但最新版本1.61.0的结果相同。使用1.4.2版本的Simple Web Server。
read_xml()可能会溢出协程的堆栈;请参阅此处关于一个类似的崩溃,该崩溃可追溯到rapidxml解析器内的64K堆栈变量。
编辑。要总结链接。。。问题的要点是,readxml中隐藏的rapidxml解析器在堆栈上分配64K,这溢出了Linux上的8K默认协程堆栈。你可以尝试两件事。。。要么迫使堆栈变量更小,例如
#define BOOST_PROPERTY_TREE_RAPIDXML_STATIC_POOL_SIZE 512
#include <boost/property_tree/xml_parser.hpp>
或者在派生协同程序时分配一个更大的堆栈(如果可以通过Simple Web Server访问)
相关文章:
- cmake如何在fedora工作站中找到boost静态库包
- "Body requirements not met"将 req 传递给 c++ 中的方法(Boost Beast 库)
- 如何在Boost::wave库中扩展token_ids以接受更多关键字
- 我可以使用 Boost.Asio 和 Boost.Beast 库发出 HTTPS 请求或 HTTP/2 请求吗?
- 任务计划程序库的预处理不起作用 - 多定义错误
- 无法构建Boost Python库(致命错误:PyConfig.H:没有此类文件或目录)
- 无法在Windows中找到来自cmake的MySQL和Boost包含/库
- 插座无法接收数据?客户使用boost.asyio库编写.服务器用Python编写
- Boost Graph库,depth_first_search未在MSVC中调用finish_edge
- 如何在Boost Graph库中的Grid_graph的边缘添加自定义属性
- 如何构建Boost Fiber库以进行分段堆栈支持
- 最新版本的 Boost asio 库中缺少方法的替代方法
- 如何在MacOS上使用Boost C 库
- 使用由 Mingw 5.3 编译的 boost ASIO 库使用 QT 时出错
- pimpl 在 C++ 应用程序/库中的什么位置使用?任何例子
- 在Linux上用于Windows的Linux上的Boost Fiber库
- 与boost::property_tree XML解析器一起使用时,boost::协同程序库崩溃
- 在Fortran程序中使用Boost Graph库(BGL)
- Boost进程库asynch_read处理程序未被调用
- 如何编译Boost.程序库