To further a workaround, I have a simpler, alternative solution to @UMD (which was making a setup2 to allocate heap memory only if not in listening mode).
Let me know if you see any “new” downsides to this one (I’m hoping to roll this out to new firmware in our fleet);
When listening mode detected, restart the device and go into listening mode before any initialisations.
Sample code looks like this:
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
retained uint8_t listenMode = 0;
void setup() {
if(listenMode){
listenMode=0;
WiFi.listen();
}
// initialisations and heap memory allocation
}
void loop () {
if(WiFi.listening()){
listenMode=1;
System.reset();
}
// do useful stuff here
}