Reputation: 1719
I have a class BLEValue
that has a member called m_accumulation
of type String
. I use this member to accumulate the data (15000 byte) received over bluetooth until the whole data is received and then this m_accumulation
will be read and the data it saves is no longer needed therefore it is set to ""
.
void BLEValue::addPart(uint8_t *pData, size_t length)
{
log_v("Adding part to m_accumulation. Current free heap: %d", ESP.getFreeHeap());
m_accumulation += std::string((char *)pData, length);
}
void BLEValue::clear()
{
m_accumulation = "";
m_accumulation.clear(); // I think it does the same as the previous line
}
The problem is that the memory allocated in the heap for this class member will not get free again after emptying m_accumulation
. I can check this using the function ESP.getFreeHeap()
. I think this is so because the object of the BLEValue
class is still alive and therefore the heap memory allocated for it will be not be freed until the object is killed. Is it true?
Is there a way to empty the heap memory allocated to this String
after reading its value without deleting the BLEValue
object completely?
Upvotes: 0
Views: 322
Reputation: 2942
Clear marks the string as having size 0, but the internal array is not necessarily changed. To force the string to free it's memory, use shrink_to_fit after clear.
Upvotes: 2