i'm successfully using the SD card lib for over a year in an automotive data logger. writing once per second, robust buffer flushing and lots of error checking. of course most SD card adapters don't have "card in place" detection so i rely on write error or read error to detect ejected/failed card. the code recovers from that just fine and logging continues without issue. except one.
each time begin is called, File.cpp calls malloc again and allocates a new buffer. i'm using 'close' after error detection, once per error (and only after error; in my application power-off is the only normal end, that's anticipated with a software timer that does flushes and closes, etc in the normal case.)
my code does once a second runtime heap size checking, and every SD.open another 96 bytes disappears.
i'd love it if i'm doing something wrong (that's easiest to fix i know that dynamic mem allocation (malloc etc) is problematic especially in embedded environments. i do it in all my programs but in an extremely limited way: once per buffer required in setup(), and never released. so it's essentially one-time static allocation at power-on time. that seems safe and has worked OK for a few years (i do 20+ hour stress testing of code and hardware).
i don't think SD lib should release memory; the object isn't being destroyed, it should just set a flag to remember it's done the malloc and reuse the buffer. but the SD code is kind of a tangle, i thought i'd ask before i attempt to rummage around in it.
tom@sensitiveresearch.com:
each time begin is called, File.cpp calls malloc again and allocates a new buffer. i'm using 'close' after error detection, once per error (and only after error; in my application power-off is the only normal end, that's anticipated with a software timer that does flushes and closes, etc in the normal case.)
There is no "File::begin()". Did you mean the File constructor that wraps an SdFile when you use SD.open()? It allocates space for the copy but frees the space when you close the File.
it's in a box (arduino Mega 2560 or DUE, an "analog" board with glue logic, and an 8-output power MOSFET board) that controls it's on DC power. it detects external power on/off and when it goes off, it does an orderly shutdown (SD flush, close, etc).
oops as i type this, thinking of john wasser's post i may see my bug...
johnwasser:
There is no "File::begin()". Did you mean the File constructor that wraps an SdFile when you use SD.open()? It allocates space for the copy but frees the space when you close the File.
i meant SD.begin(), yes, there's no File::begin() etc. i believe i'm using the library correctly.
but i think you nailed it: my screwup is i'm not closing open file(s) in the error case, if the SD card is popped out, fails, etc. i'll do so and see if the problem goes away.
my normal case is of course it NOT popped out or failing; the box powers up, logs for 10 minutes to 8 hours, power off, does an orderly shutdown, no problemo.
in case of hard failure, the box re-tries three more times only, 10 seconds apart. if that fails, it repeats only after the next power off to on cycle (the box stays on to see that). so it won't cause runaway memory "leak" in any case.
(i wish SD lib would pre-allocate room for a configurable N files to avoid malloc/free which could lead to fragmentation and silliness.)