mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-04 05:04:33 +02:00
nvs: fix memory leak when writing a blob
Introduced in 5a27a63
.
Clear usedPages list in all exit conditions, add test.
This commit is contained in:
@@ -236,11 +236,10 @@ esp_err_t Storage::writeMultiPageBlob(uint8_t nsIndex, const char* key, const vo
|
|||||||
for (auto it = std::begin(usedPages); it != std::end(usedPages); it++) {
|
for (auto it = std::begin(usedPages); it != std::end(usedPages); it++) {
|
||||||
it->mPage->eraseItem(nsIndex, ItemType::BLOB_DATA, key, ii++);
|
it->mPage->eraseItem(nsIndex, ItemType::BLOB_DATA, key, ii++);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
usedPages.clearAndFreeNodes();
|
usedPages.clearAndFreeNodes();
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize)
|
esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize)
|
||||||
{
|
{
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include "esp_partition.h"
|
#include "esp_partition.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "esp_system.h"
|
||||||
|
|
||||||
static const char* TAG = "test_nvs";
|
static const char* TAG = "test_nvs";
|
||||||
|
|
||||||
@@ -213,3 +214,28 @@ TEST_CASE("calculate used and free space", "[nvs]")
|
|||||||
TEST_ESP_OK(nvs_flash_erase());
|
TEST_ESP_OK(nvs_flash_erase());
|
||||||
TEST_ESP_OK(nvs_flash_deinit());
|
TEST_ESP_OK(nvs_flash_deinit());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("check for memory leaks in nvs_set_blob", "[nvs]")
|
||||||
|
{
|
||||||
|
esp_err_t err = nvs_flash_init();
|
||||||
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||||
|
err = nvs_flash_init();
|
||||||
|
}
|
||||||
|
TEST_ESP_OK( err );
|
||||||
|
|
||||||
|
for (int i = 0; i < 500; ++i) {
|
||||||
|
nvs_handle my_handle;
|
||||||
|
uint8_t key[20] = {0};
|
||||||
|
|
||||||
|
TEST_ESP_OK( nvs_open("test_namespace1", NVS_READWRITE, &my_handle) );
|
||||||
|
TEST_ESP_OK( nvs_set_blob(my_handle, "key", key, sizeof(key)) );
|
||||||
|
TEST_ESP_OK( nvs_commit(my_handle) );
|
||||||
|
nvs_close(my_handle);
|
||||||
|
printf("%d\n", esp_get_free_heap_size());
|
||||||
|
}
|
||||||
|
|
||||||
|
nvs_flash_deinit();
|
||||||
|
printf("%d\n", esp_get_free_heap_size());
|
||||||
|
/* heap leaks will be checked in unity_platform.c */
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user