Strung out

I have a program of about 68 kb on an arduino Due, basically it’s about 800 lines long and utilizes ten large subroutines. I communicate with Modbus field devices, various cell modems, inbound and outbound email servers, and there is also a web server to serve up html configuration pages, images, and data over a LAN. This has taken me months to make and is working great. I am finished the bulk of it and during long period testing I am encountering crashes. Nothing on the serial monitor indicates a watchdog trip or endless loop. My search for answers points to string usage and memory issues. I have used the
string1 += char(c);
scenario countless times and from what I can find this is devastating the the memory (heap no?). I walked into the infamous “string trap” I know:(. I am sure you’re all used to seeing posts about crashes and talking about string hatred and I assure you I am not looking for someone to rewrite my code and fix it because I’m too lazy. I have lost enough sleep over this to think of a few ideas to solve my issue and would like some input on them before I do some serious reworking of the various clients and servers.

I read about #include <string.h> and using those string functions(strcat() etc), I’ll lose indexOf() but worse things have happened.
How recommended and reliable is this?

There is mention of a one line fix to free(), is this doable on the Due?

I read about a reserve() function. If I use this and make a global(or not?) string that I use on all my clients and servers with lots of room to spare, say 150 bytes in size, and ensure I never exceed its limit? I would need to make it =""; often though…

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

I am currently putting myself through the paces of char arrays and learning the ins and outs as this has been an awakening slap in the face! My future code will have no mention of String.

Edit: open to ideas as well…

My search for answers points to string usage and memory issues. I have used the string1 += char(c);

What is string1? strings don't support +=. Strings do. Huge difference. Perhaps some clarification is in order, before I read the rest of your post.

Let me rephrase

String buffer; buffer += char(c);

Basically this scenario is used all over where c is coming from servers and clients and SD files.

Basically this scenario is used all over where c is coming from servers and clients and SD files.

So, lots of String causes memory fragmentation. Fixing the free() bug has no impact on that. Getting rid of Strings does.

I read about #include <string.h> and using those string functions(strcat() etc), I’ll lose indexOf() but worse things have happened.
How recommended and reliable is this?

Very and very.

You could, of course, look at the String class and see how indexOf is implemented, and create your own indexOf() function. Or, use the strchr() function that indexOf() uses (it’s a string (lower case s) function).

There is mention of a one line fix to free(), is this doable on the Due?

It is, but it is already fixed in the latest versions of the IDE. Fixing it does nothing for the memory fragmentation that is inherent in applications that do a lot of dynamic memory allocation, as the String class does.

My future code will have no mention of string.

Well, it should. String, on the other hand…

PaulS:

Basically this scenario is used all over where c is coming from servers and clients and SD files.

So, lots of String causes memory fragmentation. Fixing the free() bug has no impact on that. Getting rid of Strings does.

I read about #include <string.h> and using those string functions(strcat() etc), I’ll lose indexOf() but worse things have happened.
How recommended and reliable is this?

Very and very.

You could, of course, look at the String class and see how indexOf is implemented, and create your own indexOf() function. Or, use the strchr() function that indexOf() uses (it’s a string (lower case s) function).

There is mention of a one line fix to free(), is this doable on the Due?

It is, but it is already fixed in the latest versions of the IDE. Fixing it does nothing for the memory fragmentation that is inherent in applications that do a lot of dynamic memory allocation, as the String class does.

My future code will have no mention of string.

Well, it should. String, on the other hand…

Thanks, I am going to study string.h for now. WString.cpp has lots of ideas for work arounds I see… Any thoughts on the reserve() function? Only reason I ask is it will be the easiest to implement on my existing code.