我可以在动态分配中使用jsoncpp吗? [英] Can I use jsoncpp with dynamic allocation?

查看:113
本文介绍了我可以在动态分配中使用jsoncpp吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临着一些jsoncpp内存损坏的问题.

I'm facing some jsoncpp issues with memory corruption.

当我在本地Json :: Value变量中分配一些值时,有时它会获得错误的数据并导致崩溃.

When I assign some values in local Json::Value variable, sometimes It gets a wrong data and make a crash.

所以我正尝试使Json :: value变量具有动态分配,并更仔细地检查内存损坏.

so I'm trying to make Json::value variable with dynamic allocation and check memory corruption more carefully.

无论如何,我的问题是,我可以在动态分配中使用jsoncpp吗?并且您认为它比以前安全吗?

anyway, my question is, Can I use jsoncpp with dynamic allocation ? and Do you think it is safe than before?

我很抱歉我没有英语.

谢谢!

推荐答案

当您想要自行管理对树中值的引用或要引用值的内部时,JsonCpp可能会变得不方便. 请参阅以下代码,该代码描述了如何节省使用它的三种方式以及两个显示一些常见陷阱的示例.希望能有所帮助.

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.

请注意,在处理动态分配"时,通常智能指针非常方便,并且可以减少由于在正确的位置分配/删除对象的错误而导致的内存泄漏或内存损坏的风险.例如,授予 shared_ptr .

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;
}

这篇关于我可以在动态分配中使用jsoncpp吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆