问题
I'm facing some jsoncpp issues with memory corruption.
When I assign some values in local Json::Value variable, sometimes It gets a wrong data and make a crash.
so I'm trying to make Json::value variable with dynamic allocation and check memory corruption more carefully.
anyway, my question is, Can I use jsoncpp with dynamic allocation ? and Do you think it is safe than before?
I'm so sorry for my lacking in english.
Thank you !
回答1:
JsonCpp may get unhandy when you want to manage references to values in the tree on your own, or when you want to refer to internals of the values. See the following code that describes three ways of how it can be used savely as well as two samples that show some common pitfalls. Hope it helps a bit.
Note that when dealing with "dynamic allocation", often smart pointers are very handy and can reduce the risk of memory leaks or memory corruption due to bugs in allocating/deleting objects at the right point. Confer, for example, shared_ptr.
Json::Value createJsonValue() {
Json::Value json("a string value");
return json; // OK - enforces a copy
}
Json::Value *createJsonValueReference() {
Json::Value *json_dynamic = new Json::Value("a string value");
return json_dynamic; // OK - does not enforce a copy but keeps json value in heap
}
std::shared_ptr<Json::Value> createJsonValueSmartPointer() {
std::shared_ptr<Json::Value> result(new Json::Value("a string value"));
return result; // OK - creates a json::value on the heap and wraps it by a shared_ptr object
}
Json::Value &referenceToLocalJson() {
Json::Value json("a string value");
return json; // Not OK: Reference to stack memory associated with local variable `json` returned
}
const char* getJsonValueContent() {
Json::Value json("a string value");
return json.asCString(); // critical: reference to internals of object that will be deleted.
}
int main()
{
Json::Value copied = createJsonValue(); // will be a copy; lifetime is until end of main
Json::Value *ptr = createJsonValueReference(); // will be a reference to an object on the heap; lifetime until you call `delete ref`
std::shared_ptr<Json::Value> smartptr = createJsonValueSmartPointer(); // share_ptr object, managing a reference to an object on the heap; lifetime of shared_ptr until end of main; lifetime of referenced object until the last shared_ptr pointing to it is destroyed
Json::Value &critical = referenceToLocalJson(); // Critical; will refer to an object that has already been deleted at the end of "referenceToLocalJson"
const char* content = getJsonValueContent(); // critical: reference to internals of object that will be deleted.
cout << "copied:" << copied << std::endl;
cout << "reference:" << *ptr << std::endl;
cout << "smartptr:" << *smartptr << std::endl;
// cout << "critical:" << critical << std::endl; // undefined outcome
// cout << "content:" << content << std::endl; // undefined outcome
delete ptr; // OK - will free object referred to by ptr
// smartptr will be deleted together with the json value it refers to at the end of this function; no "explicit" delete
return 0;
}
来源:https://stackoverflow.com/questions/42828859/can-i-use-jsoncpp-with-dynamic-allocation