profile
viewpoint

Ask questionsDocument 放到另一个Document下面,为什么会core_dump?

root@NB80261026:/mnt/d/workspace/cpp/test_rapidjson# cat main.cc 
#include <string>
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include <unistd.h>

std::string Dom2String(rapidjson::Document &d)
{
     rapidjson::StringBuffer buffer;
     rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
     d.Accept(writer);
     return buffer.GetString();
}

void function(rapidjson::Document &a)
{
    rapidjson::Document b1(rapidjson::kObjectType);
    b1.AddMember("a", rapidjson::Value(1).Move(), b1.GetAllocator());
    a.AddMember("test1", b1, a.GetAllocator());
    std::cout << "in function:" << std::endl;
    std::cout << Dom2String(a) << std::endl;
    return ;
}

int main()
{
    rapidjson::Document a(rapidjson::kObjectType);
    function(a);
    usleep(1000 * 1000);
    std::cout << "out function:" << std::endl;
    std::cout << Dom2String(a) << std::endl;
    return 0;
}
root@NB80261026:/mnt/d/workspace/cpp/test_rapidjson# g++ main.cc -I /mnt/d/software/rapidjson-master/include -g -O0
root@NB80261026:/mnt/d/workspace/cpp/test_rapidjson# ./a.out 
in function:
{"test1":{"a":1}}
out function:
Segmentation fault (core dumped)
root@NB80261026:/mnt/d/workspace/cpp/test_rapidjson# 
Tencent/rapidjson

Answer questions miloyip

b1 的内存还是来自于 b1 的分配器。 需要先用 a 的分配器来复制 b1。

 a.AddMember("test1", Value(b1, a.GetAllocator()).Move(), a.GetAllocator());
useful!

Related questions

是否支持流式解析以及多个json分离解析 hot 1
是否支持流式解析以及多个json分离解析 hot 1
RapidJSON causes cc1plus: internal compiler error: Segmentation fault - rapidjson hot 1
GenericMemberIterator::Iterator implicitly declared private when RAPIDJSON_NOMEMBERITERATORCLASS is defined hot 1
Always gets a core dump while creating json string (rapidjson) hot 1
Bazel Support hot 1
Syntax typo in tutorial hot 1
是否支持流式解析以及多个json分离解析 hot 1
作者你好,我 想请教一下,这个框架是否能够将变量中文stirng 转成wchar_t? 在文档只看到定量 hot 1
travis on windows hot 1
Added headers to my project and got thousands of errors hot 1
Added headers to my project and got thousands of errors hot 1
想要的反斜杠 \. 无法表示,否则JSON解析出错 hot 1
Assertion `stack_.GetSize() == sizeof(ValueType)' failed hot 1
source:https://uonfu.com/
Github User Rank List