Search code examples
ioscocos2d-xrapidjson

Use RapidJson in Cocos2dx, it crash in Accept(writer) in IOS


I use Cocos2dx 3.9; and use rapidjson to convert CCDictionary to string; I find it will crash in value->Accept(wirter) in Real machine; but work will in Simulator.

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
value->Accept(writer);

Solution

  • It's my error, I use:

    rapidjson::Value *value = KSCCJsonRapid::jsonValueFromDictionary(dic);
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    value->Accept(writer);
    std::string jsonString = buffer.GetString();
    

    but in

    static rapidjson::Value* jsonValueFromDictionary(cocos2d::CCDictionary *dic, rapidjson::Document *document = NULL);
    

    , I create a new document, and delete it; in Real machine, the memory is limit; so the value is invalid. it can be corrected by :

    rapidjson::Document *document = new Document();
    rapidjson::Value *value = KSCCJsonRapid::jsonValueFromDictionary(dic, document);
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    value->Accept(writer);
    std::string jsonString = buffer.GetString();
    delete value;
    delete document;