gpt4 book ai didi

c++ - 使用 boost::asio::spawn 生成的 asio 处理程序中的 boost::property_tree::read_xml 段错误

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:38 27 4
gpt4 key购买 nike

以下代码在调用 boost::property_tree::read_xml() 时因段错误而崩溃。只有在使用 boost::asio::spawn() 生成的 io_service 处理程序内部调用它时才会发生这种情况。如果处理程序刚刚发布,它就可以正常工作。是否有解决方法或解决方法?( 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。

解决方案是减小池的大小,如下所示:

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

另一种解决方案是增加协程的堆栈大小。

关于c++ - 使用 boost::asio::spawn 生成的 asio 处理程序中的 boost::property_tree::read_xml 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com