I’m using Json-cpp to parse my config file and I get som weird behavior with asCString().. Can anyone explain why output of 2 is empty?
#include <iostream>
#include <fstream>
#define JSON_IS_AMALGAMATION
#include "json/json.h"
using std::cout;
using std::endl;
int main(int argc, char** argv) {
Json::Value root;
Json::Reader reader;
std::ifstream config("dev.json", std::ifstream::binary);
if (!reader.parse(config, root, false)) {
cout << "Could not parse json" << endl;
return 1;
}
std::string str = root["redis"].get("host", "localhost").asString();
const char* cstr = root["redis"].get("host", "localhost").asCString();
cout << "1:" << str << endl;
cout << "2:" << cstr << endl;
cout << "3:" << std::string(root["redis"].get("host", "localhost").asCString()) << endl;
config.close();
return 0;
}
Output:
c++ -o test test.cpp jsoncpp.cpp
1:127.0.0.2
2:
3:127.0.0.2
My json data:
{ "redis": { "host": "127.0.0.2", "port": 6379 } }
I suspect that
root["redis"].get("host", "localhost")orroot["redis"]returns aValue, not a reference to aValue. ThatValueobject will live until the end of the expression, in the case of2the temporaryValueobject will be destroyed, leavingcstras a dangling pointer. The behaviour is undefined when dereferencing a dangling pointer.In the case of
1, thestris a copy of thestd::stringreturned byasString().In the case of
3, the temporaryValuewill survive until the end of the expression (the;) allowing theconst char*returned byasCString()to be successfully processed.To solve, either:
cstrtostd::stringand it will copy the returnedconst char*, orValuereturned byget()and query it rather thanroot[].EDIT:
Based on this source, all variants of
Value.get()return aValue. So the cause is as described.