Arduino Uno crashes after abour 20 seconds sending UDP packets

The really fascinating thing is: I can send UDP packets in a (nearly) perfect fashion in loop() - using udp.beginPacket - udp.write - udp.endPacket - for hours… 10 or 12 hours as far as i’ve been testing.

But when i put this in a isolated function for testing purposes, the bitch crashes after about 30 seconds - no matter how fast or how much i try to send. And it always stumbles in the second loop (turned LED on before and off after - so LED lights when chrash appears) Code a follows:

//---------------------------------------------------------------------------
//#include <avr/wdt.h>
#include <string.h>
#include <print.h>
#include <SPI.h>
#include <Ethernet.h>

// Network
#define UDP_RX_PACKET_MAX_SIZE 128

EthernetUDP Udp;

uint8_t mymac[] = {0xC0, 0xC0, 0x04, 0x05, 0x19, 0x69};
IPAddress RemoteIP = {192, 168, 0, 99};
IPAddress LocalIP = {192, 168, 0, 120};
unsigned int RemotePort = 5432;
unsigned int LocalPort = 5432;

PROGMEM const uint8_t LEDSerial = 8;
PROGMEM const uint8_t LEDNetwork = 9;

char ReceiveBuffer[UDP_RX_PACKET_MAX_SIZE];
unsigned int ReceiveBufferPointer = 0;

PROGMEM const char test[] = "----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---";

//---------------------------------------------------------------------------
// Setup
//---------------------------------------------------------------------------
void setup() {

  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  Ethernet.begin(mymac);
  Udp.begin(LocalPort);

  for (int a = 0; a < 128; a++) {
    ReceiveBuffer[ a ] = pgm_read_byte_near(test + a);
  }
  ReceiveBuffer[127] = 0;

  Serial.begin(115200);
  //wdt_enable(WDTO_4S);
}

//---------------------------------------------------------------------------
// Distribution of incoming data
//---------------------------------------------------------------------------
void loop() {
  //wdt_reset();

  int loopRestart, packetInfo, retValue;
  unsigned int counter = 0;
  unsigned int interval = 20;
  long duration ;

  Serial.print("Starting...");
  
  duration = millis() + 40000;
  
  while (millis() < duration) {

    //wdt_reset();
    loopRestart = millis() + interval;

    int RandomLength = random(20, 128);
    counter++;

    sprintf(ReceiveBuffer, "%05d", counter);

    Udp.beginPacket(RemoteIP, RemotePort);
    Udp.write(ReceiveBuffer, RandomLength);
    Udp.endPacket();

    //Serial.println(counter, DEC);

    while (millis() < loopRestart) {
      packetInfo = Udp.parsePacket();
      delay(1);
    }
  }
  Serial.println("We made it... this time...");

  
    delay(5000);
  
}

The regular functions all run very fine… The UDP test crashes sometimes directly at startup, but most of the time after about 20 seconds - and even when i use a data rate that’s quite equal to my real application (about 16 packets/secons, 1 kb/second). The amount of data transmitted in this 20 seconds is quite different - i’ve seen anything from 2kbyte to nearly 50 kbyte, also about less than 1000 packets up to 15.000… I can’t see a pattern.

I suspect that libraries cosume resources and can’t free them fast enough without touching loop().

But it is my wish to keep such a test out of the “regular” code…

I can't see a pattern.

And we can't see all of your code. I'm going to guess, though, that you are writing outside the bounds of an array, and crapping on memory you don't own, with unpredictable results.

PaulS:
And we can't see all of your code. I'm going to guess, though, that you are writing outside the bounds of an array, and crapping on memory you don't own, with unpredictable results.

In this case it's useless to see any other code, because it's never called. As you can see the function never leaves - and as long as this code runs nothing is written. I reuse the char array over and over just with a changing length when copying.

But what i really forgot to mention: It's an W5100 ethernet shield... with the latest Wiznet libraries.

I added a complete sketch in the first post just for you - it took 30 seconds to crash, but with lots of memory....

char ReceiveBuffer[UDP_RX_PACKET_MAX_SIZE];

What is UDP_TX_PACKET_MAX_SIZE? On my install of the IDE, I have:

#define UDP_TX_PACKET_MAX_SIZE 24

So,

  for (int a = 0; a < 128; a++) {
    ReceiveBuffer[ a ] = pgm_read_byte_near(test + a);
  }
  ReceiveBuffer[127] = 0;

just shit on 104 bytes of memory you don’t own.

I don't line to say more than this:

Read Line 9: #define UDP_RX_PACKET_MAX_SIZE 128

Besides that: Wouldn't i have mentioned scrambled output? This would be for sure the very first outcome of use of unallocated memory...

Again: This is (after my editing) a complete working sketch - for about 30 seconds when it crashes.