C++ 与boost::property_树XML解析器一起使用时,boost::coroutine库崩溃

C++ 与boost::property_树XML解析器一起使用时,boost::coroutine库崩溃,c++,boost,boost-asio,boost-propertytree,boost-coroutine,C++,Boost,Boost Asio,Boost Propertytree,Boost Coroutine,我正在使用库创建简单的web服务,用于将XML转换为JSON,反之亦然。反过来,它使用了几个boost库,以及其中的boost::coroutine。对于XMLJSON转换,我使用boost::property_-tree库进行中间表示。代码如下: #include <iostream> #include <sstream> #include <server_http.hpp> #define BOOST_SPIRIT_THREADSAFE #includ

我正在使用库创建简单的web服务,用于将XML转换为JSON,反之亦然。反过来,它使用了几个boost库,以及其中的boost::coroutine。对于XMLJSON转换,我使用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 OK\r\nContent-Length: " << xml.str().length() << "\r\n\r\n" << xml.str();
    }
    catch(const exception& e)
    {
      cerr << "Error:" << endl << e.what() << endl;
      response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << 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 OK\r\nContent-Length: " << json.str().length() << "\r\n\r\n" << json.str();
    }
    catch(const exception& e)
    {
      cerr << "Error:" << endl << e.what() << endl;
      response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
    }
  };

  server.start();

  return 0;
}
#包括
#包括
#包括
#定义BOOST_SPIRIT_THREADSAFE
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost::property_树;
使用HttpServer=SimpleWeb::Server;
int main()
{
HttpServer服务器(8080,1);
server.resource[“^/json_to_xml$”][“POST”]=[](自动响应和自动请求){
尝试
{
ptree-pt;
读取json(请求->内容,pt);
ostringstreamjson,xml;
编写_json(json,pt);
clogread_xml()可能会溢出协同程序的堆栈;请参阅rapidxml解析器中的64K堆栈变量的类似崩溃

编辑。总结一下链接……要点是,嵌入read_xml中的rapidxml解析器在堆栈上分配64K,这会溢出Linux上的8K默认协同路由堆栈。您可以尝试两种方法……要么强制堆栈变量变小,例如

#define BOOST_PROPERTY_TREE_RAPIDXML_STATIC_POOL_SIZE 512
#include <boost/property_tree/xml_parser.hpp>
\define BOOST\u PROPERTY\u TREE\u RAPIDXML\u STATIC\u POOL\u SIZE 512
#包括

或者在生成协同路由时分配一个更大的堆栈(如果可以通过简单的Web服务器访问)

代码太多了。简单Web服务器似乎不是生产就绪的代码。我已经看了一段时间了,但是有太多的活动部分,很难处理。Web服务不再与不再使用boost::coroutine的简单Web服务器崩溃,但我想知道这是否是某种类型的问题关于boost::coroutine和boost::property_tree之间不兼容的问题,因为我在过去使用过这两种方法,而且我也遇到过类似的奇怪崩溃。当时我没有将其与boost::property_tree XML解析联系起来,但现在我认为可能存在某种联系。答案很好!我等了这么久。我们在我以前的公司中的生产代码已经被描述过,但是我们没有意识到问题出在RapidXML中。