Merge branch 'bugfix/event_minor_leak_v3.3' into 'release/v3.3'

esp_event: fix minor memory leak when overwriting already registered handler (v3.3)

See merge request espressif/esp-idf!5132
This commit is contained in:
Angus Gratton 2019-10-10 11:05:41 +08:00
commit 8466b8d6d3
2 changed files with 10 additions and 0 deletions

View File

@ -164,6 +164,7 @@ static esp_err_t handler_instances_add(esp_event_handler_instances_t* handlers,
if (handler == it->handler) { if (handler == it->handler) {
it->arg = handler_arg; it->arg = handler_arg;
ESP_LOGW(TAG, "handler already registered, overwriting"); ESP_LOGW(TAG, "handler already registered, overwriting");
free(handler_instance);
return ESP_OK; return ESP_OK;
} }
last = it; last = it;

View File

@ -126,6 +126,9 @@ static esp_event_loop_args_t test_event_get_default_loop_args()
static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data) static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{ {
if (!event_handler_arg) {
return;
}
simple_arg_t* arg = (simple_arg_t*) event_handler_arg; simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
xSemaphoreTake(arg->mutex, portMAX_DELAY); xSemaphoreTake(arg->mutex, portMAX_DELAY);
@ -345,7 +348,13 @@ TEST_CASE("can register/unregister handlers for all events/all events for a spec
loop_args.task_name = NULL; loop_args.task_name = NULL;
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop)); TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
/* Register the handler twice to the same base and id but with a different argument (expects to return ESP_OK and log a warning)
* This aims to verify: 1) Handler's argument to be updated
* 2) Registration not to leak memory
*/
TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, NULL));
TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg)); TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg)); TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg)); TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg)); TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));