I'm using the rapidjson C++ library, with this library you can create a JSON object. 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. 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?