Dynamic memory management: silently doing nothing is a real problem when robustness is needed
Created by: richnwhit
I'm using this excellent library in an industrial product. It has to work solidly, reliably, 100% of the time, and unfortunately I'm not getting that, due to memory problems.
Working out exactly how much memory is required is very tricky (and would be a maintenance nightmare – I wouldn’t expect someone coming along adding a new field to remember to add it to the memory capacity), and I’m working in a tiny chip so I can’t just give it loads. So I tend to measure how much it needs in a typical situation using memoryUsage, double it, and use that amount.
But I keep having problems that I can’t reproduce. It works fine in all my unit tests, and in the PC-based library, but when I run it on my target microcontroller, often the JSON will be incomplete, which I suspect is because it’s run out of memory. Sometimes there are empty objects (“{}”), and sometimes I get very long lines and my parser bombs out after 1000 characters without a linefeed.
Ideal solutions would be:
- A memory allocator that allocates memory from the heap as it's needed, rather than all at the start.
- In the existing scheme, the ability to throw an exception or at least print a warning when it runs out of memory rather than silently doing nothing. I can't think why anyone would want it to just silently do nothing!
As a bare minimum, please could someone point out where in the code it checks if it has enough memory, so I could add some code in my project?