Understanding and Fixing ESP32 LoadProhibited Errors
What causes this
The ESP32 Guru Meditation Error: LoadProhibited occurs when the CPU tries to read from a memory address that is not mapped to any RAM or is protected. This typically results from dereferencing null pointers or accessing invalid memory addresses. The exception causes the CPU to halt, and the following registers provide clues:
- EXCVADDR: Contains the address which caused the exception.
- PC: Program Counter at the time of the exception.
- PS: Processor State at the time of the exception.
In the context of ESP-IDF and FreeRTOS, these errors often arise from misconfigured tasks or incorrect use of pointers. For instance, passing an incorrect pointer to a task or using a pointer after freeing the memory can lead to a LoadProhibited error.
Minimal reproduction
Below is a minimal code snippet that triggers a LoadProhibited error by trying to dereference a null pointer:
```c
#include
#include
void faultyTask(void *pvParameters) {
int *ptr = NULL;
// Dereferencing a null pointer
int value = *ptr;
vTaskDelete(NULL);
}
void app_main() {
xTaskCreate(&faultyTask, "faultyTask", 2048, NULL, 5, NULL);
}
```
Expected serial output:
```
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x400d1234 PS : 0x00060330 A0 : 0x800d5678 A1 : 0x3ffb0000
...
EXCVADDR: 0x00000000
...
```
The fix
To resolve the LoadProhibited error, ensure that the pointers are initialized correctly and not dereferenced if they are null. Here's the corrected version of the above code:
Before:
```c
int *ptr = NULL;
int value = *ptr;
```
After:
```c
int *ptr = malloc(sizeof(int));
if (ptr != NULL) {
*ptr = 42;
int value = *ptr;
free(ptr);
}
```
By allocating memory using malloc and checking if the pointer is not null before dereferencing, we prevent the LoadProhibited error.
How SerialDoctor catches this
SerialDoctor reads the serial output from the ESP32 and immediately identifies the root cause of the LoadProhibited error. By analyzing the register values and decoding the exception, SerialDoctor can point out the exact line of code responsible for the crash. This allows developers to quickly fix the issue without manually sifting through the logs. Learn more at [serialdoctor.com](https://serialdoctor.com).
Quick checklist
- Verify all pointers are initialized before use.
- Check that memory allocations are successful with
mallocorcalloc. - Avoid using pointers after freeing their memory.
- Use static analysis tools to detect potential null pointer dereferences.
- Regularly test your code with SerialDoctor for quick diagnostics.
Seeing this crash on your board right now?
Connect it to SerialDoctor and get a root cause + code fix in under 3 seconds.
Try SerialDoctor free →