fix: invalid const and declaration ordering and added smart pointer for get

This commit is contained in:
GW_MC
2026-01-19 21:09:11 +08:00
parent 4cda7d2de3
commit 41516374f0
4 changed files with 41 additions and 36 deletions

View File

@@ -46,7 +46,7 @@ void NVSStorageHandler::put(const char* const& key, const char* const& value) {
}
}
char* NVSStorageHandler::get(const char* const& key) const {
std::unique_ptr<char[]> NVSStorageHandler::get(const char* const& key) const {
if (this->nvsHandle == 0) {
printf("NVS handle is not initialized.\n");
return nullptr;
@@ -62,11 +62,10 @@ char* NVSStorageHandler::get(const char* const& key) const {
return nullptr;
}
char* value = (char*)malloc(required_size);
err = nvs_get_str(this->nvsHandle, key, value, &required_size);
std::unique_ptr<char[]> value(new char[required_size]);
err = nvs_get_str(this->nvsHandle, key, value.get(), &required_size);
if (err != ESP_OK) {
printf("Error (%s) getting value for key %s from NVS!\n", esp_err_to_name(err), key);
free(value);
return nullptr;
}
@@ -106,7 +105,7 @@ esp_err_t NVSStorageHandler::process_all(KeyValueProcessor processor, void* arg)
}
// call the processor with the key and value
processor(arg, info.key, this->get(info.key));
processor(arg, info.key, this->get(info.key).get());
}
return ESP_OK;
}
@@ -133,7 +132,7 @@ esp_err_t NVSStorageHandler::process_filtered(const char* const& key_prefix, Key
return err;
}
// call the processor with the key and value
processor(arg, info.key, this->get(info.key));
processor(arg, info.key, this->get(info.key).get());
}
}
return ESP_OK;
@@ -162,7 +161,7 @@ esp_err_t NVSStorageHandler::process_filtered(FilterFunc filter_func, KeyValuePr
return err;
}
// call the processor with the key and value
processor(arg, info.key, this->get(info.key));
processor(arg, info.key, this->get(info.key).get());
}
}
return ESP_OK;