boost::property_tree::read_xml segfaults in an asio handler spawned using boost::asio::spawn

三世轮回 提交于 2019-12-11 01:59:37

问题


The following code crashes with a seg fault at boost::property_tree::read_xml() call. This happens only if it's called inside of an io_service handler spawned using boost::asio::spawn(). If the handler is just posted, it works ok. Is there a fix or workaround for this? (boost 1.61)

#include <boost/asio/spawn.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
#include <sstream>
#include <thread>

void process()
{
    std::cerr << "start"<< std::endl;
    std::istringstream is("<t>1</t>");
    boost::property_tree::ptree pt;
    boost::property_tree::read_xml(is, pt); // <<< seg fault here
    std::cerr << std::endl << "end" << std::endl;
}

int main()
{
    boost::asio::io_service io_service;
    boost::asio::spawn(io_service, [] (boost::asio::yield_context y){
        process();
    });
    io_service.run();
    return 0;
}

回答1:


After some digging we found that the seg fault is caused by coroutine's stack overflow because rapidxml parser used in boost::property_tree::read_xml() by default allocates 64KB on stack for the static memory pool within each xml document.

The solution is to reduce the size of the pool as follows:

#define BOOST_PROPERTY_TREE_RAPIDXML_STATIC_POOL_SIZE 512
#include <boost/property_tree/xml_parser.hpp>

Another solution would be to increase the stack size of coroutines.



来源:https://stackoverflow.com/questions/41030285/boostproperty-treeread-xml-segfaults-in-an-asio-handler-spawned-using-boost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!