2

I'm in a embedded led measuring system project now. It uses ARM & linux, and has 64M memory and 1G storage. When measuring, it's supposed to write data to a .csv file. I did it this way:

  1. Create/open a file before measurement begins
  2. In the measuring loop, when data is ready, put it into the file, then go to next measuring
  3. When user stop the measurement, the file will be closed

But, when I add this feature, the program keep running several hours, then the machine won't respond to anything ( measuring stopped, UI still display but doesn't respond to any action, etc.). And the csv file is about 15MB. While without this feature, the machine can work well all day. I've thought about this, maybe It's because the memory is used up. With such a small memory, is it possible to keep writing a file? Or should I close it every time I finished writing data? (In that case, I will have to open/close the file very frequently, it will cause our system to be slow, what is not glad to see) Apologize for my poor English, maybe someone can understand it and give me some help. God is lighting your path, thank you all!

ps: I do believe the file operations itself is correct.

the code like this:

std::ofstream out_put;
out_put.open(filePath, std::ofstream::out | std::ofstream::trunc);

while(!userStoped()){

    doSomeMesuring();

    for(int itemIndex = 0; itemIndex < itemCount; ++itemIndex){
    out_put << ',' << itemName.toStdString() << ',' 
            << data->mdata.item[itemIndex].mvalue << ',' 
            << data->mdata.item[itemIndex].judge << std::endl;
    }
}

out_put.close();
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
chinfoo
  • 45
  • 5
  • Can you provide the relevant code, or a minimal example that reproduces the error? – Björn Pollex Mar 24 '11 at 09:16
  • Can you paste in your code? Or maybe try to find a small reproducible test case that you can paste here? – sarnold Mar 24 '11 at 09:17
  • I recommend you favor C on embedded systems. Your outcomes will be far superior. Here's many good reasons: http://stackoverflow.com/questions/4352425/what-should-i-learn-first-before-heading-to-c/4352469#4352469. On top of that, there is significantly increased memory usage simply mapping in the extra tools C++ requires. – Matt Joiner Mar 24 '11 at 09:21
  • 2
    @Matt Joiner: read again. it's 64 MB, not KB. – MSalters Mar 24 '11 at 09:23
  • Where is `itemIndex` initialised/incremented? – trojanfoe Mar 24 '11 at 09:37
  • @trojanfoe My mistake, the orignal code used itemIndex, not i, I typed it wrong, please help me, thank you! – chinfoo Mar 24 '11 at 09:42
  • OK, so you've editted your source to add the itemIndex. Now where does `data` get allocated & initialised and where does it get deallocated? – trojanfoe Mar 24 '11 at 09:44
  • @chinfoo: To help with such errors, we need a complete, compilable example that reproduces the error. Otherwise we can only guess. – Björn Pollex Mar 24 '11 at 09:48
  • @trojanfoe It's none of their business, believe me, they are well treated. It's just a clip. And the data writen to the file is correct, We have examined it using some Electronic Equipments. They are not the problem. Thank you very much for helping, my expressions seems confused you. I'm just thinking that maybe the way I used the file-io is wrong ( as I said in the main post ). – chinfoo Mar 24 '11 at 09:55
  • @MStalters: What does that change? The Linux kernel itself is about 32MB, if it were in C++ it would be at least twice that. – Matt Joiner Mar 24 '11 at 12:20
  • @Matt Joiner: My company has been selling Linux-based devices that have 32 MB RAM, a Linux kernel, shell, bluetooth stack, a very real C++ application, and still they have ~4MB RAM available as application-level cache. Obviously you're not even close to being right. And if you're talking non-Linux devices, have a look at Lego's NXT2.0. 256 KB RAM, and it can still run C++ code. – MSalters Mar 31 '11 at 13:52

1 Answers1

2

You write to 'out_put', the ofstream, but never check if the stream is still valid. You could change it to

while (out_put.good() && (!userStoped())

To prove to yourself that it is the writing to a stream which is causing the problem, comment out all of the measuring code, just write lots of 'x' (or your choice of character!) to the stream to see if you have the same result.

Chris Huang-Leaver
  • 6,059
  • 6
  • 41
  • 67