Closed zekageri closed 1 year ago
You didn't provide a simple sketch to reproduce the issue, so I doubt the maintainers will be able to help.
Looks the the crash is happening in AsyncTCP, which is a different project: https://github.com/me-no-dev/AsyncTCP. I was not able to match up the line numbers with my copy of AsyncTCP. What version are you using?
Looking at AsyncTCP, I do see a potential crash if malloc() fails:
//Used to switch out from LwIP thread
static int8_t _tcp_accept(void * arg, AsyncClient * client) {
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
e->event = LWIP_TCP_ACCEPT;
e->arg = arg;
e->accept.client = client;
if (!_prepend_async_event(&e)) {
free((void*)(e));
}
return ERR_OK;
}
After the malloc there should be a line checking for failure, like this:
if(!e) return ERR_MEM;
You can monitor memory usage in the main loop like this:
void loop() {
delay(1000);
//Monitor heap usage
static uint32_t prevheap = 0;
uint32_t curheap = ESP.getFreeHeap();
bool IsChanged = false;
if (prevheap != curheap) {
static uint32_t heap_highwater_mark = 0;
static uint32_t heap_lowwater_mark = UINT32_MAX;
if (curheap < heap_lowwater_mark) {IsChanged = true; heap_lowwater_mark = curheap;}
if (curheap > heap_highwater_mark) {IsChanged = true; heap_highwater_mark = curheap;}
if(IsChanged) {
uint32_t maxblock = ESP.getMaxAllocHeap();
log_i("Heap Free %u Min %u Max %u Contig %u [%i]",
curheap, heap_lowwater_mark, heap_highwater_mark, maxblock, xPortGetCoreID());
Serial.println();
}
prevheap = curheap;
}
}
Yeah, I'm using a tweaked version of the AsyncTCP library but the base is the same. Here is the lib: https://github.com/yubox-node-org/AsyncTCPSock
I modified the following function as you suggested:
static int8_t _tcp_accept(void * arg, AsyncClient * client) {
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
if(!e) return ERR_MEM;
e->event = LWIP_TCP_ACCEPT;
e->arg = arg;
e->accept.client = client;
if (!_prepend_async_event(&e)) {
free((void*)(e));
}
return ERR_OK;
}
waiting for test results. There are a lot of random crashes unfortunatelly which all realted to tcp in some degree. Probably because of AsyncTCP. I will try it and let you know. And thank you for the heap checker function. I appreciate your help!
There are a lot of cases where the malloc-ed memory does not checked. I will put a check to every malloc i found.
is this still valid?
It seems the latest update solved this. But the function modification did not. Anyway I close this and thanks for the help.
Board
ESP32-Wrover-E
Device Description
Plain esp32
Hardware Configuration
SPI,I2C,Ethernet
Version
latest master (checkout manually)
IDE Name
PlatformIO
Operating System
Windows 11
Flash frequency
80mhz
PSRAM enabled
yes
Upload speed
115200
Description
Random crash.
Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide