Many thanks for this thread. It has helped a very frustrating problem!
But once I place the destructor in my code (or download the new zip...same thing), it doesn't function the same any more. Here's the relevant code:
void somefunction()
{
char* timestamp = isoTimestamp();
Serial.println(timestamp);
}
// iso8601 timestamp of current time
char* isoTimestamp(){
Serial.println("in isoTimestamp");
time_t t = now();
Serial.println("Allocating String(20)");
String str = String(20);
Serial.println("Formatting timestamp");
str.append(year(t));
str.append("-");
if(month(t) < 10) str.append("0");
str.append(month(t));
str.append("-");
if(day(t) < 10) str.append("0");
str.append(day(t));
str.append("T");
if(hour(t) < 10) str.append("0");
str.append(hour(t));
str.append(":");
if(minute(t) < 10) str.append("0");
str.append(minute(t));
str.append(":");
if(second(t) < 10) str.append("0");
str.append(second(t));
str.append("Z");
Serial.print("isoTimestamp: ");
Serial.println(str);
char* outputChars = str.getChars();
Serial.print("char isoTimestamp: ");
Serial.println(outputChars);
return str;
}
The above prints:
in isoTimestamp
Allocating String(20)
Formatting timestamp
isoTimestamp: 2010-06-22T23:33:23Z
char isoTimestamp: 2010-06-22T23:33:23Z
+?10-06-22T23:33:23Z
Note the two first bytes in the returned char* are wrong/odd (it isn't really a '?', but it was interpreted as a newline in the forum post). This occurs whether I "return str;" or "return str.toChars();"
I'm still pretty new to Arduino, and haven't messed with C-based stuff (aka pointers) since college long ago, so hopefully this is just a newbie mistake. But it worked fine before adding the String destructor. Well, other than the memory leak, anyway! Can someone help?
Thanks in advance.