From 05e347e80c442e578eeda551b3b1d3ca1a375141 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 18 Aug 2024 21:01:34 +0200 Subject: [PATCH] Check for memory leaks --- esp32_device/.vscode/settings.json | 3 ++- esp32_device/main/main.c | 17 +++++++++++++++++ esp32_device/main/system.c | 10 ++++++++++ esp32_device/main/system.h | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/esp32_device/.vscode/settings.json b/esp32_device/.vscode/settings.json index 2467434..d08f6b8 100644 --- a/esp32_device/.vscode/settings.json +++ b/esp32_device/.vscode/settings.json @@ -33,6 +33,7 @@ "esp_log.h": "c", "http_client.h": "c", "string.h": "c", - "cjson.h": "c" + "cjson.h": "c", + "stddef.h": "c" } } diff --git a/esp32_device/main/main.c b/esp32_device/main/main.c index 77786c3..7ba1ebd 100755 --- a/esp32_device/main/main.c +++ b/esp32_device/main/main.c @@ -17,6 +17,8 @@ void app_main(void) { esp_log_level_set("*", ESP_LOG_VERBOSE); + system_show_free_memory(); + ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device"); // Initialize storage @@ -101,6 +103,21 @@ void app_main(void) int status = secure_api_get_device_enrollment_status(); ESP_LOGI(TAG, "Current enrollment status: %d\n", status); + // Check for memory leaks + size_t before = system_show_free_memory(); + for (int i = 0; i < 1000; i++) + { + char *buff = calloc(5000, 1); + assert(storage_get_dev_name(buff) > 0); + assert(storage_get_root_ca(buff) > 0); + free(crypto_get_csr()); + // secure_api_get_device_enrollment_status(); + printf("%d\n", i); + free(buff); + } + size_t after = system_show_free_memory(); + printf("RES before = %d / after = %d / diff = %d\n", before, after, before - after); + system_sleep(120); reboot(); diff --git a/esp32_device/main/system.c b/esp32_device/main/system.c index 782a961..75bb674 100644 --- a/esp32_device/main/system.c +++ b/esp32_device/main/system.c @@ -1,15 +1,25 @@ #include "system.h" +#include "esp_log.h" #include "esp_system.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +static const char *TAG = "system"; + void system_sleep(size_t secs) { vTaskDelay((1000 * secs) / portTICK_PERIOD_MS); } +size_t system_show_free_memory() +{ + size_t v = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); + ESP_LOGI(TAG, "heap_caps_free_size(MALLOC_CAP_DEFAULT) = %d", v); + return v; +} + void reboot() { fflush(stdout); diff --git a/esp32_device/main/system.h b/esp32_device/main/system.h index ea45582..8f8d786 100644 --- a/esp32_device/main/system.h +++ b/esp32_device/main/system.h @@ -11,6 +11,11 @@ extern "C" { #endif + /** + * Get sum of free memory in chip + */ + size_t system_show_free_memory(); + /** * Sleep for a given amount of time */