Pages: [1]   Go Down
Author Topic: Freaky behaviour for equals() in WString  (Read 870 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: December 16, 2009, 09:10:16 pm by ardudillo » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12933
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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!
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
boolean String::equals(char *str)
to
Code:
boolean String::equals(const char *str)
in both WString.h and WString.cpp.

Mikal

Logged

Waterloo, Canada
Offline Offline
Full Member
***
Karma: 1
Posts: 242
Engineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Good find.  For all others interested in recursion, see here...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1261015776#4
smiley-wink
« Last Edit: December 22, 2009, 09:15:28 am by mitch_79 » Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mitch I was just about to point out that you accidentally linked to your own post when a 2x4 hit me in the head. smiley

Mikal
« Last Edit: December 22, 2009, 08:25:21 pm by mikalhart » Logged

Pages: [1]   Go Up
Jump to: