简体   繁体   中英

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

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM