How can I free a WString? (repeated memory crash)

Hello,

I am using Processing to send Strings to an LCD, after approx. 5 messages are received, the Arduino program appears to run out of memory and either reboots or freezes. I am not holding on to any past messages (at least intentionally), but every message seems to allocate more memory. How can I free the past message and stop my program from continuing to use more memory?

here is my arduino code:

#include <LiquidCrystal.h>
#include <WString.h>

#define CHARACTERSPERLINE 20
#define LCDLINES 4
#define MAXLINES 10


String heldText = String(CHARACTERSPERLINE*MAXLINES);

int received = 0;
LiquidCrystal lcd(7,8,9,10,11,12);
int buzzerPin = 3;

void setup()
{

  // start serial port at 9600 bps:
  Serial.begin(9600);
  //pinMode(2, INPUT);   // digital sensor is on digital pin 2
  lcd.begin(CHARACTERSPERLINE,LCDLINES);
  //lcd.autoscroll();
  establishContact();  // send a byte to establish contact until receiver responds 
  delay(1000);
 // heldText = "returns a new string that is a part of the original string. When using the endIndex parameter, the string between beginIndex and endIndex -1 is returned.";
  //heldTextToLCD();
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) 
  {
    // get incoming byte:
    handleIncomingChars();


    // delay 10ms to let the ADC recover:
    delay(10);    
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("0,0,0");   // send an initial string
    delay(300);
  }
}

void handleIncomingChars() {
  // read the incoming data as a char:
  char inChar = Serial.read();

  if(inChar=='\n' || inChar == '\r')
  {
    lcd.clear();
    lcd.home();
    heldTextToLCD();
    heldText = "";
    //I HAVE ALSO TRIED THE FOLLOWING TWO LINES
   // free(heldText);
   // heldText = String(CHARACTERSPERLINE*MAXLINES);
  }
  else 
  {
    // if you're not at the end of the string, append
    // the incoming character:
    if (heldText.length() < (CHARACTERSPERLINE*MAXLINES)) 
    {
      heldText.append(inChar);
    }
    else
    {
      // empty the string by setting it equal to the inoming char:
      heldText = inChar;
    }
  }
}

void heldTextToLCD()
{
  Serial.println(heldText);
  int numLines = heldText.length() / CHARACTERSPERLINE;
  //String lastFragment = "";
  int remainingLength = heldText.length();
  lcd.clear();
  int currentIndex = 0;
  int i=0;
  while(remainingLength>0 || i > MAXLINES)
  {
    
    if(i%LCDLINES==0)
   {
     lcd.clear();
   }
   else
   {
     lcd.setCursor(0,i%LCDLINES);
   }
    
    String line = String(32);//lastFragment;
    int lengthToTake = CHARACTERSPERLINE;
    
    if(remainingLength < CHARACTERSPERLINE)
    {
      lengthToTake = remainingLength;
    }
    line.append(heldText.substring(currentIndex,currentIndex+lengthToTake));
    delay(10);  
    currentIndex += lengthToTake;
    delay(100);
    lcd.print(line);
    delay(2000);
    i++;
    
   
    remainingLength = heldText.length() - currentIndex;
  }
  Serial.println("done");
  delay(10);
  Serial.println(availableMemory());
}


int availableMemory() {
  int size = 1024; // Use 2048 with ATmega328
  byte *buf;

  while ((buf = (byte *) malloc(--size)) == NULL)
    ;

  free(buf);

  return size;
}

As you can see I am using a little "availableMemory()" snippet I found, after each message I am printing that out and the number gets smaller until crashing.

I posted this on the software interfacing a couple days ago, but have since realized the issue likely isn't processing.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1255733236

I would really appreciate some help with this, thanks in advance.

I could be mistaken but I could swear I read a post in the recent past about a memory leak problem with the WString library and there was a patch given in the thread to correct it. You might try searching the posts here or perhaps someone else will recall.

Lefty

thank you so much! That was my problem. I found the thread here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241618944/12 it shows the real quick fix for the WString library's memory leak. I guess the library is missing its destructor.

Thanks again! It feels great to be able to move forward.