cac0ns3c / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

there's a memory leak in String #305

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Reusing the same string repeatedly eventually causes the program to stop 
printing.

Check out the sketch below.  By commenting out the "Lorem Ipsum" block, you can 
get the code to run forever. But with that block in, the program runs once, 
stopping before that block prints. I tried replacing the destructor with 
Xiaoyang's version, but the result was the same.

void setup() {
  Serial.begin(9600);
  Serial.println("\n\nString character functions:");

}

void loop() {

  String stringOne = "this";

  // charAt() lets you get the characters in a string:
  for (int charNum = 0; charNum < stringOne.length(); charNum++) {
    char thisChar = stringOne.charAt(charNum);
    Serial.println("Character " + String(charNum) + " of " + stringOne + " is " + thisChar);
  }

  // endsWith() checks to see if a String ends with a particular character:

  String sensorReading = "sensor = ";
  sensorReading += analogRead(0);
  Serial.print (sensorReading);
  if (sensorReading.endsWith(0)) {
    Serial.println(". This reading is divisible by ten"); 
  } 
  else {
    Serial.println(". This reading is not divisible by ten"); 

  }

  // indexOf() returns the position (i.e. index) of a particular character
  // in a string. For example, if you were parsing HTML tags, you could use it:
  stringOne = "<HTML><HEAD><BODY>";
  int firstClosingBracket = stringOne.indexOf('>');
  Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);

  stringOne = "<HTML><HEAD><BODY>";
  int secondOpeningBracket = firstClosingBracket + 1;
  int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket );
  Serial.println("The index of  the second > in the string " + stringOne + " is " + secondClosingBracket);

  // you can also use indexOf() to search for Strings:
  stringOne = "<HTML><HEAD><BODY>";
  int bodyTag = stringOne.indexOf("<BODY>");
  Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);

  stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
  int firstListItem = stringOne.indexOf("<LI>");
  int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
  Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);

  // lastIndexOf() gives you the last occurrence of a character or string:
  int lastOpeningBracket = stringOne.lastIndexOf('<');
  Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);

  int lastListItem  = stringOne.lastIndexOf("<LI>");
  Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);

   stringOne = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><p>Ipsem</p><p>Quod</p>";

// comment out these three lines and the sketch runs great.
  int lastParagraph = stringOne.lastIndexOf("<p");
  int penultimateParagraph = stringOne.lastIndexOf("<p", lastParagraph - 1);
  Serial.println("The index of the second last paragraph tag " + stringOne + " is " + penultimateParagraph);

 delay(1000);

}

Original issue reported on code.google.com by tom.i...@gmail.com on 27 Jul 2010 at 9:49

GoogleCodeExporter commented 9 years ago
Hmm, I wonder if you might be running out of RAM.  What happens if you reduce 
the size of the constant strings in the program?  

Original comment by dmel...@gmail.com on 28 Jul 2010 at 1:47

GoogleCodeExporter commented 9 years ago
Then it works.  So we need a way to know when you are running out of RAM and to 
notify you.  Is it possible to summarize the RAM a program needs at compile 
time?

Original comment by tom.i...@gmail.com on 28 Jul 2010 at 1:53

GoogleCodeExporter commented 9 years ago
We can do some checking at compile time.  This is on the to do list: 
http://code.google.com/p/arduino/issues/detail?id=40.  :)

In the meantime, I'm closing this issue.

Original comment by dmel...@gmail.com on 28 Jul 2010 at 2:09