bbcmicrobit / micropython

Port of MicroPython for the BBC micro:bit
https://microbit-micropython.readthedocs.io
Other
608 stars 285 forks source link

Memory allocation error in Script that run in older versions of MicroPython #535

Closed microbit-carlos closed 6 years ago

microbit-carlos commented 6 years ago

From: https://github.com/mu-editor/mu/issues/565

Mu 1.0.0.rc.1 Windows 10

The attached code generates the following error "Line 16 Memory Error memory allocation failed allocating 2816 bytes". No error is generated when flashing the same code using Mu 0.9.13. The program is test code for a dual 4x7-seg display with MAX7219 driver.

As the program is quite long by micro:bit standards, it is possible that the new version of Mu just takes up slightly more memory than the old version and runs out of space.

7-seg test no library with functions 2.zip

Thank you @SteveJSmythe for the testing and reporting the issue!

microbit-carlos commented 6 years ago

I have tested the code in 3 versions of MicroPython, nothing attached to the micro:bit:

dpgeorge commented 6 years ago

I can confirm that with Mu v0.9.13 the 7-seg script works, but with v1.0.0-rc.1 it doesn't.

Unfortunately I don't think it can be fixed. v1.0.0-rc.1 has about 350 bytes more heap than v0.9.13, so the issue would very likely be memory fragmentation. In v0.9.13 I guess there just happened to be a large enough memory block free due to the pattern of allocations, and in v1.0.0-rc.1 the pattern of allocations has changed and there is no longer a region available.

But I think there's a bug in the 7 segment code that leads to this MemoryError: line 16 is allocating very large bytearray's, each time around the loop it allocates an array (256 i) big, where i goes in the range 0 to 15 inclusive. I think this is a mistake and it should be allocating just two bytes with the given value (256 i). Fixing that would definitely make the script work on v1.0.0-rc.1.

SteveJSmythe commented 6 years ago

Thanks. I'll take a look at that loop.

dpgeorge commented 6 years ago

Fixed by #539