String corrupts the heap and crashes

new() was added in 1.0 of the IDE. I doubt people use it much. You need it for the STL, but anyone knowledgeable enough to use that would hold off until memory allocation is fixed.

Come to think of it, my Big Number port uses malloc and free heavily. I suppose they can't be considered reliable right now either.

@ Paul Stoffregen You are the most knowledgable as far as I can see.

Would it be too much to ask you to release an istaller for arduino boards? If it has to be re-compiled, so be it. Give us a webpage with the instructions!

I am NOT proposing a breakaway branch! Most users will be happy with the 'official' release as they will never use malloc/new or build anything neer a mission critical application. But some of us do. It would be - at least a partial - solution.

What do you think?

Will

i'm agree with bill52..thank you for your time!! :)

Bill52: Would it be too much to ask you to release an istaller for arduino boards?

That would be asking PJRC to support non-PJRC products. The Teensyduino installer is designed to never modify Arduino for non-PJRC boards.

At the moment, I'm working on a new Teensy product, so I don't have time for extra stuff lately.

It would be - at least a partial - solution.

Isn't simply copying the malloc.c I provided (which is really just a slightly modified copy from a later avr-libc) into your hardware/arduino/cores/arduino directory also a partial solution?

[quote author=Paul Stoffregen link=topic=115552.msg907746#msg907746 date=1346248294]

That would be asking PJRC to support non-PJRC products. The Teensyduino installer is designed to never modify Arduino for non-PJRC boards.

At the moment, I'm working on a new Teensy product, so I don't have time for extra stuff lately.

[/quote] I'm sorry, i must have misundertood something. From your 'long-email' below i gathered you are already supporting arduino. I understood, your installer doesn't work for arduino boards because you respect the arduino-team's decision not to include your modifications. Whatever, you viewpoint is taken.

Isn't simply copying the malloc.c I provided (which is really just a slightly modified copy from a later avr-libc) into your hardware/arduino/cores/arduino directory also a partial solution?

The "partial solution" was meant to say 'it is a solution until the arduino team provides a final fix'.

I don't know what is actually necessary to fix it. I also don't get it, if it is so simple to fix it then why don't the Arduino people do that? I am not sarcastic here, i really don't see the issue: why isn't it fixed?

There seem to be an issue with dynamic mem.alloc. and String - which is dependent on malloc. I can't remember the name - someone in the earlier posts said it is too hard to fix it. And yes, i remember you did say to copy malloc. Is this all to it? One last thing. You said something about a compiler switch? Is it a swith in the compiler to compile for arduino boards? Or for your boards? Do i/we need to be concerned abouth this switch - whatever it may be.

Thanks for your time and effort anyway.

Bill52:
I don’t know what is actually necessary to fix it.

See my comment from July 27 on this page:

http://code.google.com/p/arduino/issues/detail?id=857

Bill52:
I also don’t get it, if it is so simple to fix it then why don’t the Arduino people do that?
I am not sarcastic here, i really don’t see the issue: why isn’t it fixed?

That wasn’t my decision.

As you can see on issue 468, I tried to contribute the fix in January 2011.

Bill52:
One last thing. You said something about a compiler switch?
Is it a swith in the compiler to compile for arduino boards? Or for your boards?
Do i/we need to be concerned abouth this switch - whatever it may be.

That patch is on issue 468. It makes String more efficient.

I’m sorry, I busy with a big project, so I don’t have time to write a detailed explanation. I’ve already explained that patch many times before, so if you search enough, I’m sure you’ll find one of those explanations.

Also, if you do read my comment on issue 857, and grab the code from issue 468, you might also notice that so far absolutely nobody has done the optional step #4.

Next time you wonder why this bug still exists in Arduino for so very long, you might think about that lack of feedback?

[quote author=Nick Gammon link=topic=115552.msg873589#msg873589 date=1343337235] Here's the bug report:

http://code.google.com/p/arduino/issues/detail?id=857

You may want to comment there about your belief that it should be urgently fixed. Maybe it'll move to "implemented" in version 1.0.2.

[/quote]

Perusing this thread and the bug report linked above I note that the bug tracking shows this has now finally been "fixed".

Comment 29 by project member c.mag...@bug.st, Dec 17 (2 days ago) Done. Thank you. https://github.com/arduino/Arduino/commit/d457332664730fde14146649169b1fdfe2209514 Please check the fix.

How will I know when this bug fix has made it into the official downloadable version 1.x.x?

Thanks, John

(I hate C but love C++. Mainly e.g. because of char* vs String)

Unless it gets removed from the github repository (which would be very unusual), this should become part of 1.0.4. When (or if) 1.0.4 will release is of course difficult to predict.

Version 1.5.2 will probably release soon, so in terms of official releases, it'll probably be available in the 1.5.x series before the 1.0.x series.

Of course, if you want it on 1.0.3 or earlier, all you have to do is copy the malloc.c file. It's been used on Teensy since 0022, so it should work on all versions at least back to 0022.

Ok, thanks.

I remember last year when starting with Arduinos, finding that problem and eventually your malloc.c fix in the bug report.

A few months ago I discovered Teensy and have mostly been using them since...

Cheers, John

This malloc has problems for schedulers and RTOSes.

http://arduino.cc/forum/index.php/topic,138392.0.html

wanted to verify that we only need to copy the “WString.cpp” and “WString.h” files

fat16lib:
This example, which crashes before printing “Done”, demonstrates a serious bug in String.

#include <MemoryFree.h>

void setup() {
  Serial.begin(9600);
}
void loop() {
  int n = 0;
  while(n <=100) {
    int u = 20n;
    int v = 30
n;
    String str = String(n) + ", " + String(u) + ", " + String(v) + ", " + String(freeMemory());
    Serial.println(str);
    delay(500);
    n++;
  }
  Serial.println(“Done”); 
}



The output looks like this and the value of freeMemory() in the last column indicates a corrupt heap.


> 0, 0, 0, 1740
> 1, 20, 30, 1738
> 2, 40, 60, 13048
> 3, 60, 90, 24358
> 4, 80, 120, -29870
> 5, 100, 150, -17536
> 6, 120, 180, -3921
> 7, 140, 210, 10462
> 8, 160, 240, 23054
> 9, 180, 270, -29122
> -----crash and restart here---



The bug is from a larger program here http://arduino.cc/forum/index.php/topic,115451.0.html

I just tried the above code with Arduino IDE 1.0.4 on my Uno and it works now.

Yes. 1.0.4 includes the malloc/realloc/free fix.