Go Down

Topic: Freaky behaviour for equals() in WString (Read 943 times) previous topic - next topic

ardudillo

Dec 17, 2009, 03:09 am Last Edit: Dec 17, 2009, 03:10 am by ardudillo Reason: 1
I 've got some really weird behaviour in function equals() of the WString library. Have a look at the following simple code:

Quote

#include <WString.h>

void setup() {
  Serial.begin(9600);
  
  String s1 = "ARGH";
  String s2 = "ARGH";
  
  if (s1.equals(s2)) {
    Serial.println("blip");
  } else {
    Serial.println("blop");
  }
  
}

void loop() {
}



Now, call me optimistic but I 'd have thought it would print out "blip". Failing that, it would print "blop". Still running it gets me a blank screen on my system using Arduino Mega, IDE 0017, 64-bit kubuntu Linux. WString.h has been patched as per instructions (~String() { free(_array); } // <--- add this line) on thread:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241618944/11

although, this does not seem to be making any difference (tried both with and without the extra line).

Now, it would seem the code gets lost in the equals() function but it gets weirder. Adding some print statements to clarify what's going on the code becomes:

Quote

#include <WString.h>

void setup() {
  Serial.begin(9600);
  
  String s1 = "ARGH";
  String s2 = "ARGH";
  
  Serial.println("About to compare the strings...");
  if (s1.equals(s2)) {
    Serial.println("blip");
  } else {
    Serial.println("blop");
  }
  Serial.println("Finished comparing the strings.");
  
}

void loop() {
}



I 'd have thought this would print "About to compare the strings..." and then hang. However, my output becomes:

About to compare the strings...
About to compare the strings...
About to compare the strings...


etc, about once per second  :o

How in the name of all possible deities do I get an infinite loop in setup()? Something somewhere must be very seriously broken. Any ideas?

Coding Badly

I've been able to reproduce the problem but I cannot tell why it happens.  Even though the following should be equivalent it works while equals does not...

Code: [Select]
 const char* t1 = s1.cstr();
 const char* t2 = s2.cstr();
 if ( strcmp( t1, t2 ) == 0 ) {

I'm afraid the best I can offer is to wish you good luck!

ardudillo

Thanks, the problem can be bypassed by using contains() instead of equals() (and checking length as well if one wants to be absolutely sure). I will post it as well in the bugs section in case some developer wants to have a look.

mikalhart

Ah, this is a good one!  The explanation of what exactly is going on is perhaps too involved for this forum -- it's an infinite recursion -- but the solution is to change
Code: [Select]

boolean String::equals(char *str)

to
Code: [Select]

boolean String::equals(const char *str)

in both WString.h and WString.cpp.

Mikal


Mitch_CA

#4
Dec 22, 2009, 03:15 pm Last Edit: Dec 22, 2009, 03:15 pm by mitch_79 Reason: 1
Good find.  For all others interested in recursion, see here...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1261015776#4
;)

mikalhart

#5
Dec 23, 2009, 02:24 am Last Edit: Dec 23, 2009, 02:25 am by mikalhart Reason: 1
Mitch I was just about to point out that you accidentally linked to your own post when a 2x4 hit me in the head. :)

Mikal

Go Up