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

标签 c++ boost boost-asio boost-propertytree

以下代码在 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/

相关文章:

c++ - 使用stoi函数进行异常处理

c++ - 添加 boost 库作为 Bazel 依赖项 c++

c++ - 将 System.IO.Ports.SerialPort 代码转换为 boost::asio

c++ - 在 Windows 上显示向上/向下箭头 C++

在运行时定义的 C++ 全局 extern 常量可跨多个源文件使用

c++ - 最大subarray_problem理解

c++ -::boost::spirit: +alnum_p 和连字符

c++ - 使用 BOOST_FOREACH 时如何测试 vector 中的最后一个元素?

c++ - shared_from_this 和 this 的用例

c++ - 使用 boost::asio 时是否需要实现阻塞?