Wifi crash linked to FreeRTOS Port.c

I converted an ESP32 sample C program to C++ which basically opens the Wifi in Station mode. The program works right up to the point where I get an IP address back from the router – then crashes. I decoded my backtrace which points to freertos/port.c

Will someone please tell me what port.c is doing for Wifi? There is hardly a line of comment in that source file that says anything about its function. There is a lot of disclaimer jazz in there – nothing that gives me the bigger picture.

Here is my backtrace:

Wifi::event_handler(void*, char const*, int, void*) main/Wifi.cpp:1271 (discriminator 8)

Wifi::eventMarshaller(void*, char const*, int, void*) main/Wifi.cpp:1190

handler_execute at C:/SysGCC/esp32/esp-idf/v4.0/components/esp_event/esp_event.c:145

esp_event_loop_run at C:/SysGCC/esp32/esp-idf/v4.0/components/esp_event/esp_event.c:553 (discriminator 3)

esp_event_loop_run_task at C:/SysGCC/esp32/esp-idf/v4.0/components/esp_event/esp_event.c:115

vPortTaskWrapper at C:/SysGCC/esp32/esp-idf/v4.0/components/freertos/port.c:143

Final reference here – line 143 points to this small function:

#if CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER
// Wrapper to allow task functions to return (increases stack overhead by 16 bytes)
static void vPortTaskWrapper(TaskFunction_t pxCode, void *pvParameters)
{
pxCode(pvParameters);
//FreeRTOS tasks should not return. Log the task name and abort.
char * pcTaskName = pcTaskGetTaskName(NULL);
ESP_LOGE(“FreeRTOS”, “FreeRTOS Task “%s” should not return, Aborting now!”, pcTaskName);
abort();
}
#endif

@KeithInAsia I tried building station example in ESP-IDF with C++ and did not find any issues.

Issue you are facing generally occurs if you are returning from a task(a task should never return). Can you please confirm the same?

In my run task I do not return…

This crash traces back to my event handler. Specifically back to the IP_EVENT, IP_EVENT_STA_GOT_IP handler.

case IP_EVENT_STA_GOT_IP : { // ESP32 station got IP from connected AP
ESP_LOGI(TAG, " IP_EVENT_STA_GOT_IP");
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, “got ip:%s”, ip4addr_ntoa(&event->ip_info.ip)) ;
s_retry_num = 0 ;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}

Inside the function – the 5 lines inside – if I comment a few of them out – the crash stops.

The Log statement is always ok.

Most of the time does not crash with only Lines 2, 3 lines enabled.

Always crashes with line 4 enabled.

Always crashes with xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);

I don’t see how any of this ties back to the run task in that object…

If you built the station example in ESP-IDF with C++ then you have a lot of changes to do to make it compile. Did you change the main from .c to.cpp?