使用boost :: asio :: spawn生成的asio处理程序中的boost :: property_tree :: read_xml segfaults [英] boost::property_tree::read_xml segfaults in an asio handler spawned using boost::asio::spawn
问题描述
以下代码在boost :: property_tree :: read_xml()调用时因段错误而崩溃。
仅当在使用boost :: asio :: spawn()产生的io_service处理程序内部调用时才会发生这种情况。如果该处理程序刚刚发布,则工作正常。
是否有解决方法或解决方法?
(提升1.61)
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;
}
推荐答案
经过一番挖掘,我们发现seg错误是由协程的堆栈溢出引起的,因为默认情况下,boost :: property_tree :: read_xml()中使用的Rapidxml解析器会在堆栈中为每个xml文档中的静态内存池分配64KB。
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.
这篇关于使用boost :: asio :: spawn生成的asio处理程序中的boost :: property_tree :: read_xml segfaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!