Rapidjson C ++在对象内取消分配数组 [英] rapidjson c++ deallocate Array within Object
问题描述
我正在使用 rapidjson C ++库,通过该库,您可以创建JSON对象.目前,我遇到了一些内存问题.
I'm using the rapidjson C++ library, with this library you can create a JSON object. Currently I'm having some memory issues.
情况:
在当前设置中,我创建了一个新对象,并向其中添加了值成员和数组成员.该对象通过引用传递给多个函数,并在我的程序流程中使用.
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());
}
}
我正在循环使用同一对象,因此在再次添加成员之前先擦除该对象的成员.我正在为此使用EraseMember函数.但是,我注意到该功能没有释放数组成员的内存,因此会泄漏内存.
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. However I’ve noticed that this function isn’t releasing the memory of the array member, and thus leaks memory.
如何使Rapidjson释放具有所有成员的完整对象?
How can I make rapidjson to release the complete object with all it members?
推荐答案
RapidJSON的当前实现使用类似std::vector
的数据结构来存储对象的成员.
The current implementation of RapidJSON uses a std::vector
like data structure to store members of object.
对于您来说,删除所有成员并再次添加成员,本身不会造成泄漏.
In your case, removing all members and adding members again, does not make leak per se.
但是,由于成员的某些值是数组,因此当销毁它们时,它将调用分配器以释放内存.但是,如果您使用默认分配器rapidjson::MemoryPoolAllocator
,它不会释放内存.这将增加每次迭代的内存使用量.
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. And this will increase the memory usage for each iteration.
如果需要频繁分配/取消分配值,请改用rapidjson::CrtAllocator
.
If you need to frequently allocate/deallocate values, use rapidjson::CrtAllocator
instead.
或者,如果这些值仅在块内使用,则可以进行优化,您也可以创建本地Allocator
:
Alternatively, it can be optimised if those values are only used within the block, you may also create a local 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
}
如果buffer
在循环中足够,这种高级"用法甚至可以阻止堆分配.
This "advanced" usage can even prevent heap allocation if the buffer
is sufficient in the loop.
这篇关于Rapidjson C ++在对象内取消分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!