i try to erase all array elements within a json file and add some new elements cyclically. The problem is, that the memory of the allocator will never be deallocated after erasing so the memory usage increases by every cycle. Here is my actual code:
void DataExchanger::updateStatusList()
{
/*Remove status entries*/
removeStatusEntries();
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
/*Get actual Statuslist*/
std::list<st_Status>* stateList = etur_getStatusListe();
std::list<st_Status>::iterator it_status;
for (it_status = stateList->begin(); it_status != stateList->end(); ++it_status) {
if ((it_status->h_status == 0) && (it_status->z_status == 0))
{
continue;
}
rapidjson::Value objValue;
objValue.SetObject();
objValue.AddMember("mainstatus", it_status->h_status, allocator);
objValue.AddMember("substatus", it_status->z_status, allocator);
objValue.AddMember("level", it_status->lvl, allocator);
objValue.AddMember("isFault", (it_status->bits & 0x2) ? true : false, allocator);
objValue.AddMember("isResetlockTower", (it_status->bits & 0x40) ? true : false, allocator);
objValue.AddMember("isResetlockNacelle", (it_status->bits & 0x80) ? true : false, allocator);
jsonDoc[FILE_TAG_STATUS.c_str()].PushBack(objValue, allocator);
}
}
void DataExchanger::removeStatusEntries()
{
rapidjson::Value::ConstMemberIterator itr = jsonDoc.FindMember(FILE_TAG_STATUS.c_str());
if (itr != jsonDoc.MemberEnd())
{
for (rapidjson::Value::ConstValueIterator it = itr->value.Begin(); it != itr->value.End();)
{
const rapidjson::Value& element = (*it);
if (element.HasMember("mainstatus"))
{
it = jsonDoc[FILE_TAG_STATUS.c_str()].Erase(it);
}
}
}
}
Except the memeory issue, the code works and does what it should. Here is a snippet of the related json file:
{
"RM_Bits": 0,
"serialnumber": 0,
"statuslist": [
{
"mainstatus": 660,
"substatus": 5199,
"level": 5000,
"isFault": false,
"isResetlockTower": false,
"isResetlockNacelle": false
},
{
"mainstatus": 660,
"substatus": 5198,
"level": 5000,
"isFault": false,
"isResetlockTower": false,
"isResetlockNacelle": false
},
{
"mainstatus": 660,
"substatus": 5197,
"level": 5000,
"isFault": false,
"isResetlockTower": false,
"isResetlockNacelle": false
},
{
"mainstatus": 44,
"substatus": 5203,
"level": 30000,
"isFault": true,
"isResetlockTower": false,
"isResetlockNacelle": false
},
{
"mainstatus": 44,
"substatus": 5240,
"level": 30000,
"isFault": true,
"isResetlockTower": false,
"isResetlockNacelle": false
}
],
"warnlist": []
}
I am very grateful for any help. Thanks in advance!
EDIT: I prove the memory usage by printing out the allocators capacity against the actual usage in every cycle.
printf("Capacity: %d\t Used: %d", jsonDoc.GetAllocator().Capacity(), jsonDoc.GetAllocator().Size());
Here is an example output from the memory usage, if the array was empty and one element will be pushed back into it.
I:Capacity: 196608 Used: 132584 //Array is empty, usage is constant
I:Capacity: 196608 Used: 132584
I:Capacity: 196608 Used: 132584
I:Capacity: 196608 Used: 132584
I:Capacity: 196608 Used: 132584
I:Capacity: 196608 Used: 132584
I:Capacity: 196608 Used: 132584
I:Capacity: 196608 Used: 133096 //one element pushed back
I:Capacity: 196608 Used: 133608 //usage increasing
I:Capacity: 196608 Used: 134120
I:Capacity: 196608 Used: 134632
I:Capacity: 196608 Used: 135144
I:Capacity: 196608 Used: 135656
I:Capacity: 196608 Used: 136168
I:Capacity: 196608 Used: 136680
I:Capacity: 196608 Used: 137192
I:Capacity: 196608 Used: 137704
I:Capacity: 196608 Used: 138216
I:Capacity: 196608 Used: 138728
I:Capacity: 196608 Used: 139240
I:Capacity: 196608 Used: 139752
I:Capacity: 196608 Used: 140264
I:Capacity: 196608 Used: 140776
I:Capacity: 196608 Used: 141800
I:Capacity: 196608 Used: 142312
I:Capacity: 196608 Used: 142824
I:Capacity: 196608 Used: 143336
I:Capacity: 196608 Used: 143848
I:Capacity: 196608 Used: 144360
I:Capacity: 196608 Used: 144872
I:Capacity: 196608 Used: 145384
I:Capacity: 196608 Used: 145896 //the added element above was deleted
I:Capacity: 196608 Used: 146408 //usage is constant again
I:Capacity: 196608 Used: 146408
I:Capacity: 196608 Used: 146408
I:Capacity: 196608 Used: 146408
I:Capacity: 196608 Used: 146408
I:Capacity: 196608 Used: 146408
I:Capacity: 196608 Used: 146408
If the array is a emtpty the usage stay constant, but as soon as i push one new element to it, the usage will be increased in every cycle.