[Memory] Free allocated memory in Strings .clear() does not free it

See: https://github.com/esp8266/Arduino/issues/8485
This commit is contained in:
TD-er
2022-02-10 12:20:15 +01:00
parent f27ac68a62
commit 71963c60e3
30 changed files with 46 additions and 46 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ bool CPlugin_008(CPlugin::Function function, struct EventStruct *event, String&
case CPlugin::Function::CPLUGIN_PROTOCOL_TEMPLATE:
{
event->String1.clear();
event->String1 = String();
event->String2 = F("demo.php?name=%sysname%&task=%tskname%&valuename=%valname%&value=%value%");
break;
}
+1 -1
View File
@@ -35,7 +35,7 @@ bool CPlugin_010(CPlugin::Function function, struct EventStruct *event, String&
case CPlugin::Function::CPLUGIN_PROTOCOL_TEMPLATE:
{
event->String1.clear();
event->String1 = String();
event->String2 = F("%sysname%_%tskname%_%valname%=%value%");
break;
}
+2 -2
View File
@@ -104,8 +104,8 @@ bool CPlugin_016(CPlugin::Function function, struct EventStruct *event, String&
case CPlugin::Function::CPLUGIN_PROTOCOL_TEMPLATE:
{
event->String1.clear();
event->String2.clear();
event->String1 = String();
event->String2 = String();
break;
}
+5 -5
View File
@@ -48,9 +48,9 @@ struct C018_data_struct {
delete C018_easySerial;
C018_easySerial = nullptr;
}
cacheDevAddr.clear();
cacheHWEUI.clear();
cacheSysVer.clear();
cacheDevAddr = String();
cacheHWEUI = String();
cacheSysVer = String();
autobaud_success = false;
}
@@ -353,7 +353,7 @@ private:
}
void updateCacheOnInit() {
cacheDevAddr.clear();
cacheDevAddr = String();
if (isInitialized()) {
if (myLora->getStatus().Joined)
@@ -361,7 +361,7 @@ private:
cacheDevAddr = myLora->sendRawCommand(F("mac get devaddr"));
if (cacheDevAddr == F("00000000")) {
cacheDevAddr.clear();
cacheDevAddr = String();
}
}
}
+3 -3
View File
@@ -378,7 +378,7 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
html_TD();
addCheckBox(getPluginCustomArgName(rowCnt + 1), bitRead(line.CodeFlags, P16_FLAGS_REPEAT));
html_TD();
strCode.clear();
strCode = String();
if (line.Code > 0) {
strCode = uint64ToString(line.Code, 16); // convert code to hex for display
@@ -393,7 +393,7 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
html_TD();
addCheckBox(getPluginCustomArgName(rowCnt + 4), bitRead(line.AlternativeCodeFlags, P16_FLAGS_REPEAT));
html_TD();
strCode.clear();
strCode = String();
if (line.AlternativeCode > 0) {
strCode = uint64ToString(line.AlternativeCode, 16); // convert code to hex for display
@@ -462,7 +462,7 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
for (uint8_t varNr = 0; varNr < P16_Nlines; varNr++) {
tCommandLinesV2 line;
strError.clear();
strError = String();
// Normal Code & flags
line.CodeDecodeType = static_cast<decode_type_t>(getFormItemInt(getPluginCustomArgName(rowCnt + 0)));
+1 -1
View File
@@ -490,7 +490,7 @@ uint8_t Plugin_055_ReadChime(const String& name, String& tokens)
log += fileName;
log += ' ';
tokens.clear();
tokens = String();
fs::File f = tryOpenFile(fileName, "r");
if (f)
{
+1 -1
View File
@@ -176,7 +176,7 @@ bool do_command_case_check(command_case_data & data,
// The data struct is re-used on each attempt to process an internal command.
// Re-initialize the only two members that may have been altered by a previous call.
data.retval = false;
data.status.clear();
data.status = String();
if (!data.cmd_lc.equals(cmd_test)) {
return false;
}
+1 -1
View File
@@ -97,7 +97,7 @@ bool Modbus::handle() {
unsigned int RXavailable = 0;
#ifndef BUILD_NO_DEBUG
LogString.clear();
LogString = String();
#endif
int64_t rxValue = 0;
+1 -1
View File
@@ -300,5 +300,5 @@ void addToLogMove(uint8_t logLevel, String&& string)
Logging.add(logLevel, std::move(string));
}
// Make sure the string may no longer keep up memory
string.clear();
string = String();
}
+3 -3
View File
@@ -81,7 +81,7 @@ RamTracker::RamTracker(void) {
writePtr = 0;
for (int i = 0; i < TRACES; i++) {
traces[i].clear();
traces[i] = String();
tracesMemory[i] = 0xffffffff; // init with best case memory values, so they get replaced if memory goes lower
}
@@ -97,7 +97,7 @@ void RamTracker::registerRamState(const String& s) { // store function
int bestCase = bestCaseTrace(); // find best case memory trace
if (ESP.getFreeHeap() < tracesMemory[bestCase]) { // compare to current memory value
traces[bestCase].clear();
traces[bestCase] = String();
readPtr = writePtr + 1; // read out buffer, oldest value first
if (readPtr >= TRACEENTRIES) {
@@ -135,7 +135,7 @@ void RamTracker::getTraceBuffer() {
retval += ' ';
retval += traces[i];
addLogMove(LOG_LEVEL_DEBUG_DEV, retval);
retval.clear();
retval = String();
}
}
#endif // ifndef BUILD_NO_DEBUG
+1 -1
View File
@@ -676,7 +676,7 @@ String LoadStringArray(SettingsType::Enum settingsType, int index, String string
#endif
strings[stringCount] = tmpString;
tmpString.clear();
tmpString = String();
tmpString.reserve(estimatedStringSize);
++stringCount;
} else {
+1 -1
View File
@@ -187,7 +187,7 @@ String ReportOffsetErrorInStruct(const String& structname, size_t offset) {
* Not a member function to be able to use the F-macro
\*********************************************************************************************/
bool SettingsCheck(String& error) {
error.clear();
error = String();
#ifndef LIMIT_BUILD_SIZE
#ifdef esp8266
size_t offset = offsetof(SettingsStruct, ResetFactoryDefaultPreference);
+1 -1
View File
@@ -19,7 +19,7 @@ void ModbusRTU_struct::reset() {
delete easySerial;
easySerial = nullptr;
}
detected_device_description.clear();
detected_device_description = String();
for (int i = 0; i < 8; ++i) {
_sendframe[i] = 0;
+1 -1
View File
@@ -1092,7 +1092,7 @@ bool GetArgv(const char *string, String& argvString, unsigned int argc, char sep
int pos_begin, pos_end;
bool hasArgument = GetArgvBeginEnd(string, argc, pos_begin, pos_end, separator);
argvString.clear();
argvString = String();
if (!hasArgument) { return false; }
+2 -2
View File
@@ -278,7 +278,7 @@ void transformValue(
}
if (value == F("0")) {
value.clear();
value = String();
} else {
const int valueLength = value.length();
@@ -666,7 +666,7 @@ bool findNextDevValNameInString(const String& input, int& startpos, int& endpos,
format = valueName.substring(hashpos + 1);
valueName = valueName.substring(0, hashpos);
} else {
format.clear();
format = String();
}
deviceName.toLowerCase();
valueName.toLowerCase();
+1 -1
View File
@@ -22,7 +22,7 @@ HandledWebCommand_result handle_command_from_web(EventValueSource::Enum source,
bool handledCmd = false;
bool sendOK = false;
printWebString.clear();
printWebString = String();
printToWeb = false;
printToWebJSON = false;
+1 -1
View File
@@ -50,7 +50,7 @@ bool safeReadStringUntil(Stream & input,
const unsigned long timer = start + timeout;
unsigned long backgroundtasks_timer = start + 10;
str.clear();
str = String();
do {
// read character
+1 -1
View File
@@ -109,7 +109,7 @@ void P020_Task::discardClientIn() {
}
void P020_Task::clearBuffer() {
serial_buffer.clear();
serial_buffer = String();
serial_buffer.reserve(P020_DATAGRAM_MAX_SIZE);
}
+1 -1
View File
@@ -122,7 +122,7 @@ void P044_Task::clearBuffer() {
maxMessageSize = _min(serial_buffer.length(), P044_DATAGRAM_MAX_SIZE);
}
serial_buffer.clear();
serial_buffer = String();
serial_buffer.reserve(maxMessageSize);
}
+1 -1
View File
@@ -315,7 +315,7 @@ void P073_data_struct::NextScroll() {
}
void P073_data_struct::setTextToScroll(const String& text) {
_textToScroll.clear();
_textToScroll = String();
if (!text.isEmpty()) {
const int bufToFill = getBufferLength(displayModel);
+1 -1
View File
@@ -167,7 +167,7 @@ bool P082_data_struct::loop() {
// Full sentence received
# ifdef P082_SEND_GPS_TO_LOG
_lastSentence = _currentSentence;
_currentSentence.clear();
_currentSentence = String();
# endif // ifdef P082_SEND_GPS_TO_LOG
completeSentence = true;
} else {
+3 -3
View File
@@ -108,7 +108,7 @@ bool P087_data_struct::loop() {
for (size_t i = 0; i < length && valid; ++i) {
if ((sentence_part[i] > 127) || (sentence_part[i] < 32)) {
sentence_part.clear();
sentence_part = String();
++sentences_received_error;
valid = false;
}
@@ -117,7 +117,7 @@ bool P087_data_struct::loop() {
if (valid) {
fullSentenceReceived = true;
last_sentence = sentence_part;
sentence_part.clear();
sentence_part = String();
}
break;
}
@@ -146,7 +146,7 @@ bool P087_data_struct::getSentence(String& string) {
if (string.isEmpty()) {
return false;
}
last_sentence.clear();
last_sentence = String();
return true;
}
+2 -2
View File
@@ -114,7 +114,7 @@ bool P094_data_struct::loop() {
for (size_t i = 0; i < length && valid; ++i) {
if ((sentence_part[i] > 127) || (sentence_part[i] < 32)) {
sentence_part.clear();
sentence_part = String();
++sentences_received_error;
valid = false;
}
@@ -151,7 +151,7 @@ const String& P094_data_struct::peekSentence() const {
void P094_data_struct::getSentence(String& string, bool appendSysTime) {
string = std::move(sentence_part);
sentence_part.clear(); // FIXME TD-er: Should not be needed as move already cleared it.
sentence_part = String(); // FIXME TD-er: Should not be needed as move already cleared it.
if (appendSysTime) {
// Unix timestamp = 10 decimals + separator
if (string.reserve(sentence_part.length() + 11)) {
+1 -1
View File
@@ -313,7 +313,7 @@ void P104_data_struct::loadSettings() {
# endif // ifdef P104_DEBUG
}
buffer.clear(); // Free some memory
buffer = String(); // Free some memory
}
delete[] settingsBuffer; // Release allocated buffer
+1 -1
View File
@@ -41,7 +41,7 @@ void handle_control() {
TXBuffer.endStream();
printWebString.clear();
printWebString = String();
printToWeb = false;
printToWebJSON = false;
}
+1 -1
View File
@@ -153,7 +153,7 @@ bool handle_custom(const String& path) {
line += c;
if (c == '\n') {
addHtml(parseTemplate(line));
line.clear();
line = String();
line.reserve(128);
}
}
+2 -2
View File
@@ -265,7 +265,7 @@ void handle_root() {
addHtml(F("<TR><TD colspan='2'>Command Output<BR><textarea readonly rows='10' wrap='on'>"));
addHtml(printWebString);
addHtml(F("</textarea>"));
printWebString.clear();
printWebString = String();
}
}
html_end_table();
@@ -342,7 +342,7 @@ void handle_root() {
html_end_form();
}
printWebString.clear();
printWebString = String();
printToWeb = false;
sendHeadandTail_stdtemplate(_TAIL);
}
+2 -2
View File
@@ -57,7 +57,7 @@ void handle_tools() {
addHtml(F("<TR><TD colspan='2'>Command Output<BR><textarea readonly rows='10' wrap='on'>"));
addHtml(printWebString);
addHtml(F("</textarea>"));
printWebString.clear();
printWebString = String();
}
@@ -177,7 +177,7 @@ void handle_tools() {
html_end_form();
sendHeadandTail_stdtemplate(_TAIL);
TXBuffer.endStream();
printWebString.clear();
printWebString = String();
printToWeb = false;
}
+2 -2
View File
@@ -27,7 +27,7 @@ void handle_upload() {
"<form enctype='multipart/form-data' method='post'><p>Upload settings file:<br><input type='file' name='datafile' size='40'></p><div><input class='button link' type='submit' value='Upload'></div><input type='hidden' name='edit' value='1'></form>"));
sendHeadandTail_stdtemplate(true);
TXBuffer.endStream();
printWebString.clear();
printWebString = String();
printToWeb = false;
}
@@ -64,7 +64,7 @@ void handle_upload_post() {
addHtml(F("Upload finished"));
sendHeadandTail_stdtemplate(true);
TXBuffer.endStream();
printWebString.clear();
printWebString = String();
printToWeb = false;
}
+1 -1
View File
@@ -130,7 +130,7 @@ bool WebTemplateParser::process(const char c) {
} else if (Tail == contentVarFound) {
processVarName();
}
varName.clear();
varName = String();
}
}
break;