Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By sillyfrog
#57727 Hi,

I have found what appears to be a bug - somewhere/somehow when working with large arrays. Below is the smallest test case I've come up with, and I'm not sure where to go from here. Please excuse me if I've done something silly!

The attached generates an array of 2,000 elements, of type "unsigned long" (the same for int, but I think these are the same size on the ESP8266 with Arduino). This is about 8k of RAM, and I should have 40k available for local variables. I have peppered the code with yield() and delay() statements to take that out of the picture. I have tested this only on the WeMos D1 Mini v1 and v2.1.0 (all I have). The attached code reliably crashes the chip. The traceback reason is not consistent, and the stack trace can be very very large.

This was part of a bigger program, but I have cut it right down for the simplest test case I could find.

Some things I have noted:
    If I remove the line Serial.println(tmp);, I don't have any issues.
    If I set i=2000 (ie: the size of the array), rather than i=0 on line 31, I don't have any issues.
However I'm guessing this is due to complier optimisations (ie: the code is getting stripped out).

The thing that's really confusing me is if I uncomment line 34, things work fine. Comment it out, I get a crash. I thought maybe it was because the array was not initialised, hence lines 23-25 (not that it should matter as the memory is allocated by this stage). I have also added a number of extra slots to MAX_SIGNAL_LEN during the deceleration just to be sure (I have no clue what's happening, so trying everything!)

So if anyone has any thoughts, ideas, things I can try, that would be great!

Thanks in advance!

My setup:
    Board: WeMos D1 R2 & mini
    CPU Frequency: 80Mhz
    Flash Size: "4MB (3MB SPIFFS)
You do not have the required permissions to view the files attached to this post.
User avatar
By sillyfrog
#57915 So after looking further, speaking with others and more testing, I found the issue - I expect it's due to a stack overflow or similar.

If I change the following line:
Code: Select all  unsigned long sig[MAX_SIGNAL_LEN+10];

To:
Code: Select all  static unsigned long sig[MAX_SIGNAL_LEN+10];


The problem goes away (my understanding is "static" will allocate this in the heap at startup).

This of course means there is less program memory 100% of the time, but for not crashing, I can live with that!
User avatar
By David Van Brink
#58529 I just created my forum account to report this same bug, and confirm a similar resolution.
(And "Hi, everyone", sorry my first post is to complain, the ESP8266 arduino support is actually pretty awesome, and I've benefited a lot from reading here.)

The code is below, and it's intermittent (maybe depends on contents of uninitialized memory). But making the big array static fixes it. (So far!)

I'm guessing that the stack space per process is smallish. Could possibly also fix by dynamically allocate with malloc or new object... but I didn't want to go there.

Code: Select all// david van brink 2016-11-22 crashing example, wemos d1 mini

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80); // probably uses a chunk of space.

void setup()
{
  Serial.begin(115200);
  Serial.print("\n\nhello there\n");

  char z2[20000];
//  static char z2[20000];  // this makes it work ok
  Serial.printf("z2 is %d\n", (int)z2);
}

void loop()
{
  delay(100);
}
User avatar
By RichardS
#58532 Of course in one case you put 20,000 bytes on the stack, in the other case its in global ram space, but it will take away from available heap! :-(

You need to implement smart memory usage to keep everyone happy if you have a large program.... normally done with malloc(), I have great success in doing it this way.....

RichardS