Skip to content

Commit

Permalink
Merge pull request #105 from espressif/fix/cdc_mem_leakage
Browse files Browse the repository at this point in the history
fix(tusb_cdc): Added freeing cdc object
  • Loading branch information
roma-jam authored Dec 20, 2024
2 parents 555db55 + cda86e8 commit 113058d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions device/esp_tinyusb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## (Unreleased)

- CDC-ACM: Fixed memory leak on deinit

## 1.5.0

- esp_tinyusb: Added DMA mode option to tinyusb DCD DWC2 configuration
Expand Down
17 changes: 15 additions & 2 deletions device/esp_tinyusb/tusb_cdc_acm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -295,6 +295,16 @@ static esp_err_t alloc_obj(tinyusb_cdcacm_itf_t itf)
return ESP_OK;
}

static esp_err_t obj_free(tinyusb_cdcacm_itf_t itf)
{
esp_tusb_cdc_t *cdc_inst = tinyusb_cdc_get_intf(itf);
if (cdc_inst == NULL || cdc_inst->subclass_obj == NULL) {
return ESP_FAIL;
}
free(cdc_inst->subclass_obj);
return ESP_OK;
}

esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg)
{
esp_err_t ret = ESP_OK;
Expand Down Expand Up @@ -331,7 +341,10 @@ esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg)

esp_err_t tusb_cdc_acm_deinit(int itf)
{
return tinyusb_cdc_deinit(itf);
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_ERROR(obj_free(itf), TAG, "obj_free failed");
ESP_RETURN_ON_ERROR(tinyusb_cdc_deinit(itf), TAG, "tinyusb_cdc_deinit failed");
return ret;
}

bool tusb_cdc_acm_initialized(tinyusb_cdcacm_itf_t itf)
Expand Down

0 comments on commit 113058d

Please sign in to comment.