ESP-MQTT Memory leak

charliemb
Posts: 3
Joined: Fri Sep 08, 2023 1:16 pm

ESP-MQTT Memory leak

Postby charliemb » Wed Sep 13, 2023 11:54 am

When trying to initialise and start the mqtt client within a FreeRTOS task, I experience memory leaks/corruption. These are the two commands run:

Code: Select all

  esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);

Code: Select all

esp_mqtt_client_start(received_client);
This happens even if I allocate over 50% of the heap to the task. I understand FreeRTOS operates on a task based stack and that these commands do their own dynamic heap allocation. However they are writing beyond the bounds and writing into variables from other components and task stacks somehow.

This seems to be a bug with the only workaround to not do it within a task. However, I would like to do it in a task so I can keep retrying without holding up the rest of my code.

Has anyone else experienced anything similar?

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-MQTT Memory leak

Postby ESP_Sprite » Thu Sep 14, 2023 1:34 am

You seem to be confusing heap and stack and it's not entirely clear what your actual issue is. Can you post your full code and a log, if possible including backtrace?

charliemb
Posts: 3
Joined: Fri Sep 08, 2023 1:16 pm

Re: ESP-MQTT Memory leak

Postby charliemb » Sun Sep 17, 2023 4:15 pm

I can't post my entire code as some of it is used commercially.

This is the code to initialise the client:

Code: Select all


void Setup() {
  const esp_mqtt_client_config_t mqtt_cfg = {
      .broker =
         ...
          },
  };

  esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);

  if (client) {
    vTaskDelay(pdMS_TO_TICKS(5000));
    xQueueSend(setup_queue, &client, 0);
  }
}
This is the task that starts the client:

Code: Select all

void StartClient(void* pvParameters) {
  ESP_LOGI(TAG, "MQTT Task started");

    EventBits_t bits =
        xEventGroupWaitBits(wifi::wifi_event_group, wifi::kWifiConnectedBit,
                            pdFALSE, pdTRUE, portMAX_DELAY);
    if (bits & wifi::kWifiConnectedBit) {
      ESP_LOGI(TAG, "WiFi ready - attempting to connect to MQTT");
      esp_mqtt_client_handle_t received_client;
      if (xQueueReceive(setup_queue, &received_client, portMAX_DELAY)) {
        esp_mqtt_client_start(received_client);
      }
    }
}
If I start the code with a massive allocated stack depth, the memory writes over other locations.

Code: Select all

xTaskCreate(StartClient, "mqtt", 106542, NULL, 1, &mqtt_task_handle);
I only know this is the case as it overwrites an objects variable which is used as the iterator in a loop, and when MQTT is started in this way, it creates an infinite loop in that component. I can see from printing the memory address and value that it gets set to some extremely large integer only after the MQTT is started (visible because of the delays I put in).

I apologise for getting confused with stack/heap, it's a little confusing as FreeRTOS allocates a stack for a task on to the heap, or am I understanding this wrong?

Thank you.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-MQTT Memory leak

Postby ESP_Sprite » Mon Sep 18, 2023 2:40 am

Yes, you're right, FreeRTOS allocates task stack from the heap. Wrt the comment that you made with initializing the MQTT stack outside of a task: there's no such thing; your app_main already runs in a task. Your issue smells like some memory corruption in your own code (as the mqtt stack is tested and tried and normally does not have these issues), but unfortunately it's a bit hard to debug if you can't give us more information.

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP-MQTT Memory leak

Postby MicroController » Mon Sep 18, 2023 10:28 am

Until seeing any more code, I suggest checking the uxItemSize used in the initialization of setup_queue.

charliemb
Posts: 3
Joined: Fri Sep 08, 2023 1:16 pm

Re: ESP-MQTT Memory leak

Postby charliemb » Mon Sep 18, 2023 11:33 am

Thank you for the pointers. The codebase is large and as I say I'm not really able to show much more. How would you go about diagnosing these sorts of issues if I suspect something else in the code is causing it? From my testing everything else works fine if the MQTT component is commented out, hence my suspicion.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-MQTT Memory leak

Postby ESP_Sprite » Tue Sep 19, 2023 1:09 am

It's hard to say without specific backtraces etc. If the issue is reproducible, you can potentially start with connecting the entire thing to a debugger, set a watchpoint on the iterator variable, see if you can track down where it is written.

Who is online

Users browsing this forum: No registered users and 123 guests