Memory issue, it crash

Dear All, good evening!

I have an issue and apparentley is due to the memory. May program run but after a short time it stop or it creash/restart from the stup() function.

I fund that function to test the memory. I do not remember where I found it but it has been useful.

static void freeRAM (){
  extern int __heap_start, *__brkval;
  int v;
  int free = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  Serial.print(F("Free RAM : "));
  Serial.println(free); // Which is the unit?
}

At the startup() I added freeRAM() and it display 581.
When the GPS position are collected, feeRAM display 193.
Then the sendGPRS() will send to a remote server the coord. At the begining of the sendGPRS, freeRAM display 33.
Then it stop or crash.

I suppose I do not have enough RAM, it the reason why it crashes.

How can I manage my RAM?
Can we free it?

Here is my setup

void setup()
{
  //Initialize serial ports for communication with computer
  Serial.begin(TERMBAUD);
  cell.begin(GPRSBAUD);
  
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);

  #ifdef DEBUG
    Serial.println(F(" "));
    Serial.println(F("*************************************"));
    Serial.println(F("* Communication... *"));
    Serial.println(F("*************************************"));
    Serial.println(F(" "));
  #endif

}

This happen since I change

    //while (uart_gps.available()) OLD
    while(Serial.available()) // NEW

The GPS coord are collected like this:

void checkGPS(void)
{

 // uart_gps.begin(GPSBAUD);

  
  #ifdef DEBUG
    Serial.println("");
    Serial.println(F("GPS gathering data... (processGps)"));
  #endif

  // Parse GPS data for 2 second
  for (unsigned long start = millis(); millis() - start < 2000;){
    
    //while (uart_gps.available())
    while(Serial.available())
    {
      char c = Serial.read();
      //char c = uart_gps.read();
      // New valid NMEA data available
      if (gps.encode(c)) 
      {
        newGpsData = true;
      }
    
    }
  }
}

Finally, my sendGPRS function

void sendGPRS(){
  //cell.println("AT+SBAND=6");
  freeRAM();
  #ifdef DEBUG
    Serial.println(F(""));
    Serial.println(F("Attaching GPRS..."));
  #endif
  cell.println("AT+CGATT=1");

// IT CRASHes HERE!!!!

  freeRAM();
  waitFor("OK");
  freeRAM();
  
  //#ifdef DEBUG
    Serial.println(F("Setting up PDP Context..."));
  //#endif
  cell.println("AT+CGDCONT=1,\"IP\",\""+apn+"\"");
  waitFor("OK");
 
  //#ifdef DEBUG
    Serial.println(F("Activating PDP Context..."));
  //#endif
  
  cell.println("AT+CGACT=1,1");
  waitFor("OK");
  
  //#ifdef DEBUG
    Serial.println(F("Configuring TCP connection to TCP Server..."));
  //#endif
  cell.println("AT+SDATACONF=1,\"TCP\",\""+ip+"\",80");
  waitFor("OK");
  
  //#ifdef DEBUG
    Serial.println(F("Starting TCP Connection..."));
  //#endif
  cell.println("AT+SDATASTART=1,1");
  waitFor("OK");

[... code ...]
}

Any idea to solve my problems???

Many thank to all.

Cheers

Post the whole of your code.

Mark

OK, but it's a lot....
http://www.hello-web.net/temp/forum.txt

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
http://arduino.cc/forum/index.php/topic,145765

Thank Niks for that information.

But that post is old! July 2012!
Do you know when it can be fix?

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Ok, I will look for that but I am a bginner, may I ask you some tip and hints? some link?

Thank a lot!!

This class may help?
#include <PString.h>

I heart once that it is no relevable....

I have an exemple without "strcpy, strcat, strcmp, etc." but it need PString....

But that post is old! July 2012!

I guess that depends on your definition of old. 9 months ago is not old in my opinion.

There is a note on the Change log page that says that the issue is addressed in 1.0.4, but that version is not yet available.

And, no, the PString class is not the answer. It is no better than the String class.

Be a big boy and learn to use char arrays and string functions, like people have for 40 years.