简体   繁体   English

Rapidjson C ++在对象内取消分配数组

[英]rapidjson c++ deallocate Array within Object

I'm using the rapidjson C++ library , with this library you can create a JSON object. 我正在使用Rapidjson C ++库 ,通过此库,您可以创建JSON对象。 Currently I'm having some memory issues. 目前,我遇到了一些内存问题。

The situation: 情况:

In my current setup I've created a new object, and added value members and an array member to it. 在当前设置中,我创建了一个新对象,并向其添加了值成员和数组成员。 The object is passed by reference to multiple functions and used in the flow of my program. 该对象通过引用传递给多个函数,并在我的程序流程中使用。

rapidjson::Value data;
data.SetObject();

while(...)
{
    // --------------------------
    // Add coordinates to object

    JSON::AllocatorType& allocator = data.GetAllocator();

    JSONValue region;
    region.SetArray();
    region.PushBack(rectangle.m_x1, allocator);
    region.PushBack(rectangle.m_y1, allocator);
    region.PushBack(rectangle.m_x2, allocator);
    region.PushBack(rectangle.m_y2, allocator);

    data.AddMember("regionCoordinates", region, allocator);

    // --------------------------
    // Add number of changes

    data.AddMember("numberOfChanges", numberOfChanges, allocator);

    ... call function and pass data
    ... call function2 and pass data

    if(data.MemberBegin() != data.MemberEnd())
    {
        data.EraseMember(data.MemberBegin(), data.MemberEnd());
    }
}

I'm using the same object in a loop, and thus erasing the members of the object before I'm adding the members again. 我在循环中使用同一对象,因此在再次添加成员之前先擦除对象的成员。 I'm using the EraseMember function for this. 我为此使用EraseMember函数。 However I've noticed that this function isn't releasing the memory of the array member, and thus leaks memory. 但是,我注意到该函数没有释放数组成员的内存,因此会泄漏内存。

How can I make rapidjson to release the complete object with all it members? 我怎样才能使Rapidjson释放具有所有成员的完整对象?

The current implementation of RapidJSON uses a std::vector like data structure to store members of object. RapidJSON的当前实现使用类似std::vector数据结构来存储对象的成员。

In your case, removing all members and adding members again, does not make leak per se. 就您而言,删除所有成员并再次添加成员本身不会造成泄漏。

However, since some values of members are array, when they are destructed, it will call the allocator to free the memory. 但是,由于成员的某些值是数组,因此在销毁它们时,它将调用分配器以释放内存。 But if you are using the default allocator rapidjson::MemoryPoolAllocator , it does not free the memory. 但是,如果您使用默认分配器rapidjson::MemoryPoolAllocator ,则它不会释放内存。 And this will increase the memory usage for each iteration. 这将增加每次迭代的内存使用量。

If you need to frequently allocate/deallocate values, use rapidjson::CrtAllocator instead. 如果您需要频繁分配/取消分配值,请改用rapidjson::CrtAllocator

Alternatively, it can be optimised if those values are only used within the block, you may also create a local Allocator : 另外,如果仅在块内使用这些值,则可以对其进行优化,您也可以创建一个本地Allocator

char buffer[1024];
Value::Allocator localAllocator(buffer, sizeof(buffer));
while (...)
{
   Value region;
   region.SetArray();
   region.PushBack(rectangle.m_x1, localAllocator);

   // ...
   localAllocator.Clear(); // Only available for MemoryPoolAllocator
}

This "advanced" usage can even prevent heap allocation if the buffer is sufficient in the loop. 如果循环中的buffer足够,这种“高级”用法甚至可以阻止堆分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM