2016-07-29 27 views
0

次の方法で問題が発生しています。私が間違っていることを理解するために何か助けが必要です。ドキュメント値への参照を返すRapidjson

ドキュメント内の値への参照を返したいとします。関数の外からDocumentを渡しているので、jsonファイルを読み込んだときに "失わない"ようにしています。

私はこのような呼び出しています
const rapidjson::Value& CTestManager::GetOperations(rapidjson::Document& document) 
{ 
    const Value Null(kObjectType); 

    if (m_Tests.empty()) 
     return Null; 

    if (m_current > m_Tests.size() - 1) 
     return Null; 

    Test& the_test = m_Tests[m_current]; 

    CMyFile fp(the_test.file.c_str()); // non-Windows use "r" 
    if (!fp.is_open()) 
     return Null; 

    u32 operations_count = 0; 

    CFileBuffer json(fp); 
    FileReadStream is(fp.native_handle(), json, json.size()); 

    if (document.ParseInsitu<kParseCommentsFlag>(json).HasParseError()) 
    { 
     (...) 
    } 
    else 
    { 
     if (!document.IsObject()) 
     { 
      (...) 
     } 
     else 
     { 
      auto tests = document.FindMember("td_tests"); 
      if (tests != document.MemberEnd()) 
      { 
       for (SizeType i = 0; i < tests->value.Size(); i++) 
       { 
        const Value& test = tests->value[i]; 

        if (test["id"].GetInt() == the_test.id) 
        { 
         auto it = test.FindMember("operations"); 
         if (it != test.MemberEnd()) 
         { 
          //return it->value; is this legitimate? 
          return test["operations"]; 
         } 

         return Null; 
        } 
       } 
      } 
     } 
    } 

    return Null; 
} 

Document document; 
auto operations = TestManager().GetOperations(document); 

私は関数内test["operations"]の値を検査するとき、私は私が(すみかコードから削除デバッグコード)期待するすべてのものを見ることができます。

関数の外で返される値を調べると、配列(これは私が期待している)であることがわかります。メンバcount int配列も正しいですが、それを印刷すると、代わりにガーベッジが表示されます。

メソッド内の文字列に値を「印刷」すると、期待通りのものが得られます(つまり、形式の整ったjson)が、すべてのキーの外側にある場合は、「IIIIIIII」のように表示され、 t文字列が正しく表示されます。このよう

rapidjson::StringBuffer strbuf2; 
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer2(strbuf2); 
    ops->Accept(writer2); 

は、私は、パラメータとして値を受信する方法を変更することを決めた仕事と私は、このように呼んでいるこの

u32 CTestManager::GetOperationsEx(rapidjson::Document& document, rapidjson::Value& operations) 
{ 
    (...) 

    if (document.ParseInsitu<kParseCommentsFlag>(json).HasParseError()) 
    { 
     (...) 
    } 
    else 
    { 
     if (!document.IsObject()) 
     { 
      (...) 
     } 
     else 
     { 
      auto tests = document.FindMember("tests"); 
      if (tests != document.MemberEnd()) 
      { 
       for (SizeType i = 0; i < tests->value.Size(); i++) 
       { 
        const Value& test = tests->value[i]; 

        if (test["id"].GetInt() == the_test.id) 
        { 
         const Value& opv = test["operations"]; 

         Document::AllocatorType& allocator = document.GetAllocator(); 
         operations.CopyFrom(opv, allocator); //would Swap work? 
         return operations.Size(); 
        } 
       } 
      } 
     } 
    } 

    return 0; 
} 

のようにそれに深いコピーをしませんでした。

Document document; 
Value operations(kObjectType); 
u32 count = TestManager().GetOperationsEx(document, operations); 

しかし...私は同じことを得る!!!!

私はそれが愚かなものになるだろうが、私はそれに私の手を置くことはできません知っている!

アイデア?

答えて

0

この場合の問題は、ParseInSituの使用にあります。 GetOperationsのいずれかが存在する場合、​​はスコープを失い、クリーンアップされます。 jsonは、ファイルへのバッファが移動するときにin-situで解析されるため、データも移動します。

関連する問題