Pages: 1 [2] 3   Go Down
Author Topic: Arduino resets unexpectedly - NOT running out of SRAM  (Read 1924 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're point blank condemnation of the String class has me really worried. I use them everywhere. I know they're expensive in program space, and in heap, but that expense is included in my free memory results, and I find them much easier to work with.

When I said to upload the sketch as an attachment, I meant to click on "Additional Options" and upload the whole thing. That saves multiple posts.

Anyway, as soon as I see the String class, I can see a very large potential problem. Especially here:

Code:
String listenForInstruction(){
  String inst;

Every time this function is called it makes a String, and then it returns one. So this is thrashing strings. There was a bug report a while back about free not properly freeing memory, which may possibly not be fixed yet.

Even if it is, the reports of free memory can be misleading. You might conceivably have 900 bytes free but they may be 450 x 2 byte slots. And it crashes when it needs a 3 byte slot.

I don't care how easy they are to work with, that will be the problem, almost certainly. Stuff like this could be easily rewritten to not use the String class:

Code:
inst=String(GBT_serInString) ;
  if ((inst=="-2") || (inst=="-3")) return "";  // gives -3 at end of file, -2 beyond end

eg.

Code:
if (strcmp (GBT_serInString, "-2") == 0 || strcmp (GBT_serInString, "-3") == 0)
  return "";
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If Strings are that flakey why are they on the Arduino reference pages withou any health warning? What in particular goes wrong with them? Is there somewhere to read up on the issues?
 

I presume you read this (sticky)? Read this before posting a programming question

On that page it mentions:

Quote
Hint: The String class tends to gobble up memory. Try to avoid using that, especially in larger programs.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Guys - many thanks for the input.

Nick I read your sticky, but managed to gloss over the hint - sorry. I'm now terrified of Strings! Damn. I'm going to do a series of tests in the morning.

I'll use a 9volt battery at all times, hopefully to eliminate voltage loss as possible cause. I'm going to take the MUX and Gobetwino out of the picture for now. Then I'm going to let it fail (hopefully!) using only the serial Monitor. This my starting point. Then the tests...

Comment out Serial.read(s) - 1 line only - easy test
Comment out all but one Serial.print - again easy to do  - will eliminate potential problem but will still show the resets
remove all Strings -- aagghh

maybe some ZZZs will help
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys

Did a load of tests. Strings are indeed the culprit.

It is nothing to do with the size of the program - even tiny programs can fail.
It is nothing to do with recursion, deep nesting, or the general expense of the String overhead.
It is nothing to do with fragmentation of the heap.

It is simply that the implementation of Strings has a bug which corrupts the Free List (and some fixed string data in the process). You can see it by printing out the heap, and by walking the Free List.

When the corruption occurs, it can cause system failure (system hangs, or system resets), or it can have no apparent effect. If you are printing to the Serial Monitor (or another device on the serial port), you may get a warning of impending collapse – fixed character strings will have one or more initial characters replaced by nulls. Eg A loop containing Serial.println(‘”Hello World”); will start producing “©©llo World”. (the ©© will be little squares).

I’ll give you initial evidence and analysis if I get time tomorrow. Then I’ll try put some limits on when this corruption does and doesn’t occur. It may only occur where String parameters are passed.

Nick - you mentioned an old bug report. Who does the debugging? Where are the bug reports reported- they may save me some time.

Many thanks for steering me in the right direction.
Logged

Canada
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Frequently Befuddled
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just came across this thread and reading through to the solution I wanted to add - I ran into a similar problem with String objects, and it seems that whatever caused it, it was introduced with version 1.0 - I have an Arduino Mega that had been running a sketch fine for over 6 months, compiled on 022. I had used Strings quite a bit, but there were never any problems.

Then after 1.0 came out, I updated all my libraries and made some changes to the project running on the Mega. After that, I started getting random hangs and restarts, and there was almost always a 'clue' just before it died. As Kenny said, there would be a string with the first character lost. At first it would happen in one spot, so I'd 'fix' or change that bit of code, then it would happen somewhere else, etc. till finally I rewrote the sketch to remove every last String object.

After removing all the Strings, it's been rock-solid ever since (over 3 months now without any problems.)

Cheers!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick - you mentioned an old bug report. Who does the debugging? Where are the bug reports reported- they may save me some time.

See this, in particular page 2:

http://arduino.cc/forum/index.php?topic=95914.15

It's been reported here:

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

Maybe give them a boost on that bug report page? Mention how long you spent tracking it down? It is only medium priority and has not yet "accepted" status.

Quote
I'm now terrified of Strings!

...

Quote
Strings are indeed the culprit.

Be afraid!
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I then incorporated code from the forum (many thanks) to test for free memory, and all is well

If this is the code I'm thinking of, it looks for the size of the gap between the stack and the heap. This is fine for confirming that there is space available for the stack to grow into. I don't know how the heap is managed in Arduino, and depending how it's done this 'free' space may not be available for heap allocation; in that case you might have more or less heap space 'available' than indicated as free memory.

I normally prefer to leave memory management to the runtime environment, but on something as tight as Arduino I think memory management is too important to be entrusted to the runtime. Just don't use dynamic memory allocation. In a system like this, it's asking for trouble.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick

Thanks for the links. Seems I'm on a well traveled road! I have just boosted the bug report.

My time has not been wasted since I was new to C, Arduino, Gobetwino, etc, and the process of digging around for code to expose the issue has helped me learn a lot, BUT...       (This is not a rant, even if it reads like one!!).

I find the overall experience puzzling, and worrying. There is a known serious bug right at the heart of the project, the DMA. And we know apparently that Strings use DMA. I'll try an analogy: for Arduino brand=Ford, Arduino board=Car, Strings=automatic gearbox, DMA=cogwheel, Arduino Reference page= Showroom, SRAM=oil.

Ford sell cars. There are numerous reports of problems with their automatic gearboxes. They are traced to defective cogwheels. The cars occasionally crash. Various customers chat amongst themselves and suggest that perhaps automatics aren't such a great idea, they're a bit expensive on oil, etc. Most are oblivious to the true danger within. Ford continues to advertise the automatic feature in their showroom, telling you exactly how it works.

Now someone finds out the the same cogwheels are used throughout the car. The problem gets put on some engineer's todo list and gets a medium priority!!!! OMG. Meanwhile customers around the country are puzzled as to why their cars keep crashing. Most mechanics tell them to check the oil.

There are always bugs, but this is a doozer. It's the fact that the DMA free list is at the heart of of things that worries me most. Microcontrolers control real things that can be dangerous, in my case a kiln. The doubts are surfacing ... Is this a genuine development environment, or just a hobby playground? Is Arduino a trustworthy babysitter for my kiln?

If I can put some limits in the next few hours on the where Strings go amiss, I'll post the results. Then back to my project to sanitize it.

Please do what you can to get the Reference pages changed. Without specific knowledge of where Strings do and dont work, Strings are a complete liability and IMHO should not be offered up as a working feature by appearing there. Same applies to DMA, but at least that's not on the first page you read !!  (Should Arduino library developers be warned?)

What about a specific place on the Playground/Forum for known bugs, and how to avoid them? I'd read it. Not everything on the bug list of course, but serious bugs likely to trip up the average user.
And while we're at it, what about a specific place on the Playground/Forum for known C poo-traps for the uninitiated, and how to avoid them? I'd read it, and it might seriously cut down on repetitive Forum traffic.

Enough already. Cheers.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Peter - as you can see I've learned some more! Follow Nicks links for a revealing tale.
If this is the code I'm thinking of, it looks for the size of the gap between the stack and the heap.
I used code to walk the free list as well.
Just don't use dynamic memory allocation. In a system like this, it's asking for trouble.
You're now preaching to the converted!! But as you can see from the links, the String library uses DMA, and who knows what other libraries use it!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There is a known serious bug right at the heart of the project, the DMA.
There is no DMA - Direct Memory Access.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New Jersey
Offline Offline
Faraday Member
**
Karma: 49
Posts: 3420
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There is a known serious bug right at the heart of the project, the DMA.
There is no DMA - Direct Memory Access.
I believe he's referring to Dynamic Memory Allocation.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never heard it called that, certainly not with its own identical acronym.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

typical - just spotted the bug list on the Playground!! - ignore that suggestion

Sorry about the accronym - thought I'd seen it referred to as DMA in discussion s of memory management - maybe not.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 49
Posts: 3420
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

typical - just spotted the bug list on the Playground!! - ignore that suggestion

Sorry about the accronym - thought I'd seen it referred to as DMA in discussion s of memory management - maybe not.

I didn't recognize it either, but Wikipedia agrees with you.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My background is in systems engineering and embedded systems, where Direct Memory Access is the more common meaning (and has been for at least thirty years); I'd never seen dynamic memory allocation referred to as DMA.
Apologies.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: 1 [2] 3   Go Up
Jump to: