Crash when returning a char array from a function

1

A freind and I are working together on an ESP32 webserver, and we have hit a wall. My buddie has pretty much given up on it, and I am less skilled than him.

We have the following functions, which compile fine, but cause the ESP32 to crash. After lots of debugging I am pretty certain the crash occurs when trying to return c

void handle_logs_view(String path){
    char** list = sdcard_list_files(path);
    for (int i=0;list[i]!=NULL;i++){
        Serial.println(list[i]);
    }
}

char** sdcard_list_files(String path){
  Serial.println("Listing files for "+path);
  if (path.compareTo("/")){
      char* c[]={"dir1","file1.log","file2.log","file3.log","file4.log","file5.log",NULL};
      return c;
  }
  return NULL;
}
#endif

The results from the exception decoder are as follows:

PC: 0x400d1d1b: handle_logs_view(String) at C:\Users\MickW\Downloads\Front_End-11_create_file_explorer\Front_End-11_create_file_explorer\sample_code\WIFITest/WIFITest.ino line 45
EXCVADDR: 0x00000000

Decoding stack results
0x400d1d1b: handle_logs_view(String) at C:\Users\MickW\Downloads\Front_End-11_create_file_explorer\Front_End-11_create_file_explorer\sample_code\WIFITest/WIFITest.ino line 45
0x400d1e7e: std::_Function_handler   >::_M_invoke(const std::_Any_data &) at C:\Users\MickW\AppData\Local\Temp\arduino_build_351256\sketch\WebServer.cpp line 32
0x400d68ff: std::function ::operator()() const at c:\users\mickw\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-97-gc752ad5-5.2.0\xtensa-esp32-elf\include\c++\5.2.0/functional line 2271
0x400d69a5: FunctionRequestHandler::handle(WebServer&, HTTPMethod, String) at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\libraries\WebServer\src\detail/RequestHandlersImpl.h line 45
0x400d6a12: WebServer::_handleRequest() at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\libraries\WebServer\src\WebServer.cpp line 633
0x400d6b8d: WebServer::handleClient() at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\libraries\WebServer\src\WebServer.cpp line 314
0x400d1d6e: loop() at C:\Users\MickW\Downloads\Front_End-11_create_file_explorer\Front_End-11_create_file_explorer\sample_code\WIFITest/WIFITest.ino line 22
0x400d8ad5: loopTask(void*) at C:\Users\MickW\Documents\Arduino\hardware\espressif\esp32\cores\esp32\main.cpp line 23
0x40089552: vPortTaskWrapper at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/port.c line 143

This is from the espressif website:

LoadProhibited, StoreProhibited
This CPU exception happens when application attempts to read from or write to an invalid memory location. The address which was written/read is found in EXCVADDR register in the register dump. If this address is zero, it usually means that application attempted to dereference a NULL pointer.

I would be greatful for any help or advice

c++
arduino
esp32
asked on Stack Overflow Apr 26, 2021 by Mickrige McMahon • edited Apr 27, 2021 by ocrdu

2 Answers

4

You are returning a pointer to an automatic (i.e. stack-based) variable. That variable goes away when sdcard_list_files returns, leaving the pointer 'dangling`.

One solution is to declare c as static. Another (in C++) would be to return a std::optional <std::array<std::string>, 6>>.

answered on Stack Overflow Apr 26, 2021 by Paul Sanders • edited Apr 26, 2021 by Paul Sanders
1

c should be declared static, preferably at the top level of your code. In your example, c is declared on the stack and will disappear when sdcard_list_files() returns. Declaring c with the static attribute will fix that.

answered on Stack Overflow Apr 26, 2021 by Logicrat

User contributions licensed under CC BY-SA 3.0