Arduino frozen after 4 String concatenation

Hi,
during an experiment I've found that my Arduino MEGA ADK get frozen after 4 String concatenation inside the loop function. This happens after one loop execution.
I'm compiling the code on Arduino IDE 1.0 on Windows 7 64bit.

Here the sample code:

void setup() {
Serial.begin(9600);
Serial.println("Ready");
}

void loop() {
Serial.println("Creating myString");

String myString = String("abcdefg");
myString += ",";
myString += String(123);
myString += String(123);

Serial.print("myString: ");
Serial.println(myString);

Serial.println("done!");

delay(1000);
}

Here's the output:

Ready
Creating myString
myString: abcdefg,123123
done!
Creating myString

What I don't understand is why this happens after one loop execution.

Other observations:
Replacing the String initialization with this one (removed the g char):

  String myString = String("abcdef");

the program run fine.

If I allocate a second myString2 variable (initialized like before with "abcdef"), it works fine:

void setup() {                
  Serial.begin(9600);
  Serial.println("Ready");
}

void loop() {
  Serial.println("Creating myString");
  
  String myString = String("abcdef");
  myString += ",";
  myString += String(123);
  myString += String(123);
 
  Serial.print("myString: ");
  Serial.println(myString);
  
  Serial.println("done!");
  
  String myString2 = String("abcdef");
  myString2 += ",";
  myString2 += String(123);
  myString2 += String(123);
 
  Serial.print("myString2: ");
  Serial.println(myString2);
  
  Serial.println("done!");
  
  delay(1000);
}

So a memory size problem can be excluded, right?

tested with an Duemillanova win7/64

IDE 0.22 runs fine

IDE 1.0 blocks quite fast with the following output

Ready
Creating myString
myString: abcdefg,123123
done!
Creating myString
myString: abcdefg,     123
done!
Creating myString

Can you try with the 0.22/0.23 IDE ?

This is still happening on Arduino Mega ADK with Arduino 1.0.3 for this code:
...
Serial.println("step 6");
logMessage= String();
logMessage=String(" Left: ")+String(straight_left1)+String(" : ")+String(straight_left2)+String (" = ")+String(median_left)+String (" L_Spd: ")+String(left_speed)+ String (" R_Spd: ")+String(right_speed) ;
Serial.println("step 7");
...
After few iterations, everything stops between "step 6" and "step 7"

Did anybody found a solution for this?

sorin_63:
Did anybody found a solution for this?

Don't use Strings.

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

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