Check for memory leaks

This commit is contained in:
Pierre HUBERT 2024-08-18 21:01:34 +02:00
parent 38197afd79
commit 05e347e80c
4 changed files with 34 additions and 1 deletions

View File

@ -33,6 +33,7 @@
"esp_log.h": "c",
"http_client.h": "c",
"string.h": "c",
"cjson.h": "c"
"cjson.h": "c",
"stddef.h": "c"
}
}

View File

@ -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();

View File

@ -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);

View File

@ -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
*/