From 4ec521ccf55ea8bc992d4a589a2fc290d3e52bb1 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 8 Jul 2023 13:06:59 +0700 Subject: [PATCH] Fix memory issue #270 because of network reconnection. --- library.json | 2 +- library.properties | 2 +- src/ESP_Mail_Client.cpp | 2 +- src/ESP_Mail_Client.h | 2 +- src/ESP_Mail_Client_Version.h | 4 ++-- src/ESP_Mail_Const.h | 2 +- src/ESP_Mail_Error.h | 2 +- src/ESP_Mail_FS.h | 2 +- src/ESP_Mail_IMAP.h | 12 ++++++++---- src/ESP_Mail_Print.h | 2 +- src/ESP_Mail_SMTP.h | 12 ++++++++---- src/extras/MB_Time.h | 2 +- src/extras/RFC2047.cpp | 2 +- src/extras/RFC2047.h | 2 +- src/wcs/esp32/ESP32_TCP_Client.cpp | 13 +++++++++---- src/wcs/esp32/ESP32_TCP_Client.h | 10 ++++++++-- src/wcs/esp32/ESP32_WCS.cpp | 6 +++--- src/wcs/esp32/ESP32_WCS.h | 4 ++-- src/wcs/esp8266/ESP8266_TCP_Client.cpp | 11 ++++++++--- src/wcs/esp8266/ESP8266_TCP_Client.h | 10 ++++++++-- 20 files changed, 67 insertions(+), 37 deletions(-) diff --git a/library.json b/library.json index 3316a973..2fd77bb7 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP Mail Client", - "version": "3.1.14", + "version": "3.1.15", "keywords": "communication, email, imap, smtp, esp32, esp8266, samd, arduino", "description": "Arduino E-Mail Client Library to send, read and get incoming email notification for ESP32, ESP8266 and SAMD21 devices. The library also supported other Arduino Devices using Clients interfaces e.g. WiFiClient, EthernetClient, and GSMClient.", "repository": { diff --git a/library.properties b/library.properties index c0dc10a1..c0ed6e39 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP Mail Client -version=3.1.14 +version=3.1.15 author=Mobizt diff --git a/src/ESP_Mail_Client.cpp b/src/ESP_Mail_Client.cpp index daa90954..be0c2ca2 100644 --- a/src/ESP_Mail_Client.cpp +++ b/src/ESP_Mail_Client.cpp @@ -2,7 +2,7 @@ #define ESP_MAIL_CLIENT_CPP #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_Client.h b/src/ESP_Mail_Client.h index 9230ab12..2c77c664 100644 --- a/src/ESP_Mail_Client.h +++ b/src/ESP_Mail_Client.h @@ -2,7 +2,7 @@ #define ESP_MAIL_CLIENT_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_Client_Version.h b/src/ESP_Mail_Client_Version.h index 735101bd..659d824a 100644 --- a/src/ESP_Mail_Client_Version.h +++ b/src/ESP_Mail_Client_Version.h @@ -3,8 +3,8 @@ #ifndef ESP_MAIL_VERSION -#define ESP_MAIL_VERSION "3.1.14" -#define ESP_MAIL_VERSION_NUM 30114 +#define ESP_MAIL_VERSION "3.1.15" +#define ESP_MAIL_VERSION_NUM 30115 /* The inconsistent file version checking to prevent mixed versions compilation. */ #define VALID_VERSION_CHECK(ver) (ver == ESP_MAIL_VERSION_NUM) diff --git a/src/ESP_Mail_Const.h b/src/ESP_Mail_Const.h index 4a36bac3..216c74ce 100644 --- a/src/ESP_Mail_Const.h +++ b/src/ESP_Mail_Const.h @@ -6,7 +6,7 @@ #define ESP_MAIL_CONST_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_Error.h b/src/ESP_Mail_Error.h index af2f7c0d..47a446c2 100644 --- a/src/ESP_Mail_Error.h +++ b/src/ESP_Mail_Error.h @@ -7,7 +7,7 @@ #define ESP_MAIL_ERROR_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_FS.h b/src/ESP_Mail_FS.h index 394cff42..a7a2057e 100644 --- a/src/ESP_Mail_FS.h +++ b/src/ESP_Mail_FS.h @@ -6,7 +6,7 @@ #define ESP_MAIL_CONFIG_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_IMAP.h b/src/ESP_Mail_IMAP.h index abaab3da..ede84b45 100644 --- a/src/ESP_Mail_IMAP.h +++ b/src/ESP_Mail_IMAP.h @@ -3,14 +3,14 @@ #define ESP_MAIL_IMAP_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif /** * Mail Client Arduino Library for Espressif's ESP32 and ESP8266, Raspberry Pi RP2040 Pico, and SAMD21 with u-blox NINA-W102 WiFi/Bluetooth module * - * Created July 7, 2023 + * Created July 8, 2023 * * This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers. * @@ -4930,8 +4930,12 @@ bool IMAPSession::handleConnection(Session_Config *session_config, IMAP_Data *im return MailClient.handleIMAPError(this, TCP_CLIENT_ERROR_NOT_INITIALIZED, false); } - if (connected()) - MailClient.closeTCPSession((void *)this, false); + // Resources are also released if network disconnected. + if (!MailClient.reconnect(this)) + return false; + + // Need to close previous connection first to free resources. + MailClient.closeTCPSession((void *)this, false); _session_cfg = session_config; _imap_data = imap_data; diff --git a/src/ESP_Mail_Print.h b/src/ESP_Mail_Print.h index 0da9a9d5..a69157aa 100644 --- a/src/ESP_Mail_Print.h +++ b/src/ESP_Mail_Print.h @@ -2,7 +2,7 @@ #define ESP_MAIL_PRINT_H_ #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_SMTP.h b/src/ESP_Mail_SMTP.h index 4379d28b..058dda5f 100644 --- a/src/ESP_Mail_SMTP.h +++ b/src/ESP_Mail_SMTP.h @@ -3,14 +3,14 @@ #define ESP_MAIL_SMTP_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif /** * Mail Client Arduino Library for Espressif's ESP32 and ESP8266, Raspberry Pi RP2040 Pico, and SAMD21 with u-blox NINA-W102 WiFi/Bluetooth module * - * Created July 7, 2023 + * Created July 8, 2023 * * This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers. * @@ -3411,9 +3411,13 @@ bool SMTPSession::handleConnection(Session_Config *session_config, bool &ssl) if (!client.isInitialized()) return MailClient.handleSMTPError(this, TCP_CLIENT_ERROR_NOT_INITIALIZED); } + + // Resources are also released if network disconnected. + if (!MailClient.reconnect(this)) + return false; - if (connected()) - MailClient.closeTCPSession((void *)this, true); + // Close previous connection first to free resources. + MailClient.closeTCPSession((void *)this, true); _session_cfg = session_config; diff --git a/src/extras/MB_Time.h b/src/extras/MB_Time.h index 289c2612..d9e39283 100644 --- a/src/extras/MB_Time.h +++ b/src/extras/MB_Time.h @@ -2,7 +2,7 @@ #define MB_Time_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/extras/RFC2047.cpp b/src/extras/RFC2047.cpp index 718c7a4a..b3214ee9 100644 --- a/src/extras/RFC2047.cpp +++ b/src/extras/RFC2047.cpp @@ -2,7 +2,7 @@ #define RFC2047_CPP #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/extras/RFC2047.h b/src/extras/RFC2047.h index d0126b1a..5dc924a0 100644 --- a/src/extras/RFC2047.h +++ b/src/extras/RFC2047.h @@ -4,7 +4,7 @@ #define RFC2047_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30114) +#if !VALID_VERSION_CHECK(30115) #error "Mixed versions compilation." #endif diff --git a/src/wcs/esp32/ESP32_TCP_Client.cpp b/src/wcs/esp32/ESP32_TCP_Client.cpp index 74c03d2f..e37c4258 100644 --- a/src/wcs/esp32/ESP32_TCP_Client.cpp +++ b/src/wcs/esp32/ESP32_TCP_Client.cpp @@ -1,7 +1,7 @@ /* - * ESP32 TCP Client Library v2.0.12 + * ESP32 TCP Client Library v2.0.13 * - * Created June 17, 2023 + * Created July 8, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -152,7 +152,7 @@ void ESP32_TCP_Client::setInsecure() bool ESP32_TCP_Client::ethLinkUp() { - if (strcmp(ETH.localIP().toString().c_str(), "0.0.0.0") != 0) + if (validIP(ETH.localIP())) { ETH.linkUp(); return true; @@ -160,6 +160,11 @@ bool ESP32_TCP_Client::ethLinkUp() return false; } +bool ESP32_TCP_Client::validIP(IPAddress ip) +{ + return strcmp(ip.toString().c_str(), "0.0.0.0") != 0; +} + void ESP32_TCP_Client::ethDNSWorkAround() { } @@ -173,7 +178,7 @@ bool ESP32_TCP_Client::networkReady() return networkStatus; #else - return WiFi.status() == WL_CONNECTED || ethLinkUp(); + return (WiFi.status() == WL_CONNECTED && validIP(WiFi.localIP())) || ethLinkUp(); #endif } diff --git a/src/wcs/esp32/ESP32_TCP_Client.h b/src/wcs/esp32/ESP32_TCP_Client.h index 9f341ece..83cc0984 100644 --- a/src/wcs/esp32/ESP32_TCP_Client.h +++ b/src/wcs/esp32/ESP32_TCP_Client.h @@ -1,7 +1,7 @@ /* - * ESP32 TCP Client Library v2.0.12 + * ESP32 TCP Client Library v2.0.13 * - * Created June 17, 2023 + * Created July 8, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -99,6 +99,12 @@ class ESP32_TCP_Client */ bool ethLinkUp(); + /** + * Checking for valid IP. + * @return true for valid. + */ + bool validIP(IPAddress ip); + /** * Ethernet DNS workaround. */ diff --git a/src/wcs/esp32/ESP32_WCS.cpp b/src/wcs/esp32/ESP32_WCS.cpp index 52d5c26c..69603c3d 100644 --- a/src/wcs/esp32/ESP32_WCS.cpp +++ b/src/wcs/esp32/ESP32_WCS.cpp @@ -1,7 +1,7 @@ /* - * ESP32 WiFi Client Secure v2.0.5 + * ESP32 WiFi Client Secure v2.0.6 * - * Created July 4, 2023 + * Created July 8, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -390,7 +390,7 @@ bool ESP32_WCS::connected() { if (!_ssl->client) - return 0; + return false; return _ssl->client->connected(); } diff --git a/src/wcs/esp32/ESP32_WCS.h b/src/wcs/esp32/ESP32_WCS.h index 8a144f87..195739c1 100644 --- a/src/wcs/esp32/ESP32_WCS.h +++ b/src/wcs/esp32/ESP32_WCS.h @@ -1,7 +1,7 @@ /* - * ESP32 WiFi Client Secure v2.0.5 + * ESP32 WiFi Client Secure v2.0.6 * - * Created July 4, 2023 + * Created July 8, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) diff --git a/src/wcs/esp8266/ESP8266_TCP_Client.cpp b/src/wcs/esp8266/ESP8266_TCP_Client.cpp index b24df3b9..dbeb72a9 100644 --- a/src/wcs/esp8266/ESP8266_TCP_Client.cpp +++ b/src/wcs/esp8266/ESP8266_TCP_Client.cpp @@ -1,8 +1,8 @@ /** * - * ESP8266 TCP Client Library v2.0.13 + * ESP8266 TCP Client Library v2.0.14 * - * Created June 17, 2023 + * Created July 8, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -198,6 +198,11 @@ bool ESP8266_TCP_Client::ethLinkUp() return ret; } +bool ESP8266_TCP_Client::validIP(IPAddress ip) +{ + return strcmp(ip.toString().c_str(), "0.0.0.0") != 0; +} + void ESP8266_TCP_Client::ethDNSWorkAround() { @@ -244,7 +249,7 @@ bool ESP8266_TCP_Client::networkReady() return networkStatus; #else - return WiFi.status() == WL_CONNECTED || ethLinkUp(); + return (WiFi.status() == WL_CONNECTED && validIP(WiFi.localIP())) || ethLinkUp(); #endif } diff --git a/src/wcs/esp8266/ESP8266_TCP_Client.h b/src/wcs/esp8266/ESP8266_TCP_Client.h index 55d8dc4c..0e110655 100644 --- a/src/wcs/esp8266/ESP8266_TCP_Client.h +++ b/src/wcs/esp8266/ESP8266_TCP_Client.h @@ -1,8 +1,8 @@ /** * - * The Network Upgradable ESP8266 Secure TCP Client Class, ESP8266_TCP_Client.h v2.0.13 + * The Network Upgradable ESP8266 Secure TCP Client Class, ESP8266_TCP_Client.h v2.0.14 * - * Created June 17, 2023 + * Created July 8, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -96,6 +96,12 @@ class ESP8266_TCP_Client */ bool ethLinkUp(); + /** + * Checking for valid IP. + * @return true for valid. + */ + bool validIP(IPAddress ip); + /** * Ethernet DNS workaround. */