Issue with Code (Memory Leak)

Hi everybody,

i am having issue with my code....
It stops running after couple of minutes.
I am using Arduino Uno and integrated the Ethernet Shield.

I believed there is a memory leakage happening because i have run counter to see if it will continue running.

Can someone please check my code. And what will i do to fix the issue or maybe free the memory.

Thanks,

Code Below:

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xAB, 0xBE, 0xEF, 0xFE, 0xED, 0xDE };
byte ip[] = { 192, 168, 0, 114 }; //web client
byte server[] = { 192, 168, 0, 113 }; // web server

EthernetClient client;

void setup()
{

Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);

Serial.println("connecting...");

if (client.connect(server, 8080)) {
Serial.println("connected");
client.println("GET /evacuation/index.php HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}

void loop()
{
char *data = new char[strlen("location=M4&ip_address=192.168.0.114&status=HIGH")+1];
strcpy(data, "location=M4&ip_address=192.168.0.114&status=HIGH");

if (client.connected())
{
if (digitalRead(8))
{
//Serial.println(count);

    }
    else
    {
      if(client.connect(server,8080))
      {
        client.println("POST /evacuation/add.php HTTP/1.0");
        client.println("Host:  192.168.0.113");
        client.println("Content-Type: application/x-www-form-urlencoded;");
        client.print("Content-Length: ");
        client.println(strlen(data));
        client.println();
        client.println(data);
        Serial.println(data);
        Serial.println("Status sent to Web Server");
        Serial.println("Resetting Memory");
        
      }

    }
      delete (data);
      delay (1000);//end delay before loop starts again

}
}

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's unreadable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)


what's the point of the dynamic memory allocation here

char *data = new char[strlen(“location=M4&ip_address=192.168.0.114&status=HIGH”)+1];
strcpy(data, “location=M4&ip_address=192.168.0.114&status=HIGH”);

Dynamic memory should not be used with small controllers like Uno. Use a fixed buffer of the same size.

Also don't forget to always delete dynamic memory after use.

there must be problem in void loop()
as your code stops after couple of minutes
so once try with void loop()

sorry for that

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xAB, 0xBE, 0xEF, 0xFE, 0xED, 0xDE };
byte ip[] = { 192, 168, 0, 114 }; //web client
byte server[] = { 192, 168, 0, 113 }; // web server



EthernetClient client;

void setup()
{
  

  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  delay(1000);

  Serial.println("connecting...");
  
  if (client.connect(server, 8080)) {
    Serial.println("connected");
    client.println("GET /evacuation/index.php HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  char *data = new char[strlen("location=M4&ip_address=192.168.0.114&status=HIGH")+1];
  strcpy(data, "location=M4&ip_address=192.168.0.114&status=HIGH");
  
  
  if (client.connected()) 
  {
      if (digitalRead(8))
        {
         //Serial.println(count);

        }
        else
        {
          if(client.connect(server,8080))
          {
            client.println("POST /evacuation/add.php HTTP/1.0");
            client.println("Host:  192.168.0.113");
            client.println("Content-Type: application/x-www-form-urlencoded;");
            client.print("Content-Length: ");
            client.println(strlen(data));
            client.println();
            client.println(data);
            Serial.println(data);
            Serial.println("Status sent to Web Server");
            Serial.println("Resetting Memory");
            
          }

        }
          delete (data);
          delay (1000);//end delay before loop starts again
          
  }
}

You dynamically allocate everytime in loop(). You only delete it if a condition is met. As you unconditionally allocate, you will also have ot unconditionally delete.

And as said, don't use dynamic allocation.

data is always allocated but destroyed only if client is connected.

I also wonder why client.connect is called if client is already connected.
But I'm not a network expert...

which board are you using

I have tried it before with arduino 2560, same issue. But now uno

I am sending this data to mysql database through network

No need to create data then copy the text literal to it, just create the char array and initialize it with the text. The way you are doing it now uses ram to store the text, then additional ram for data, doubling the memory needed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.